yugelaile 2014-02-17 05:12 采纳率: 0%
浏览 1000

如何用c#代码实现将本地文件上传到百度文库

用c#代码实现将本地文件上传到百度文库 有研究过这个的吗 希望能帮助下 谢谢

  • 写回答

1条回答

  • Olin_ଲ 2023-03-06 13:16
    关注
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class BaiduWenkuUploader {
        private static final String UPLOAD_API_URL = "https://wenku.baidu.com/api/upload";
    
        public static void uploadFile(String filePath) throws IOException {
            File file = new File(filePath);
            String fileName = file.getName();
    
            // 创建HTTP连接
            URL url = new URL(UPLOAD_API_URL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0");
            conn.setRequestProperty("Referer", "https://wenku.baidu.com/");
    
            // 设置请求正文
            InputStream inputStream = new FileInputStream(file);
            byte[] fileBytes = inputStream.readAllBytes();
            String boundary = "------------------------" + Long.toHexString(System.currentTimeMillis());
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            conn.setRequestProperty("Content-Length", Integer.toString(fileBytes.length));
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Cache-Control", "no-cache");
    
            // 构造请求正文
            String prefix = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n\r\n";
            String suffix = "\r\n--" + boundary + "--\r\n";
            byte[] prefixBytes = prefix.getBytes();
            byte[] suffixBytes = suffix.getBytes();
            byte[] contentBytes = new byte[fileBytes.length + prefixBytes.length + suffixBytes.length];
            System.arraycopy(prefixBytes, 0, contentBytes, 0, prefixBytes.length);
            System.arraycopy(fileBytes, 0, contentBytes, prefixBytes.length, fileBytes.length);
            System.arraycopy(suffixBytes, 0, contentBytes, prefixBytes.length + fileBytes.length, suffixBytes.length);
    
            // 发送请求
            conn.getOutputStream().write(contentBytes);
    
            // 处理响应
            int responseCode = conn.getResponseCode();
            String responseBody = conn.getResponseMessage();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("文件上传成功!");
            } else {
                System.out.println("文件上传失败:" + responseBody);
            }
        }
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名