aalijie 2014-12-02 03:15 采纳率: 0%
浏览 8464

http协议无法上传超过10M文件到服务器

上传10M以下的没有问题,上传10M以上的就无法从服务器获得响应值,这是为什么,http协议如何实现大文件上传

URL url = new URL(ManageConfigUtils.IMG_UPLOAD_PATH);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type",
ContentType.multipart_form_data);
con.setRequestProperty("Charset", "UTF-8");
con.setInstanceFollowRedirects(true);
con.setDoOutput(true);
con.setDoInput(true);
con.setReadTimeout(300 * 1000);
con.setChunkedStreamingMode(1024 * 1024);
con.connect();

        StringBuffer buffer = new StringBuffer();
        buffer.append("\r\n--LamfireFormBoundaryucJiylDzwZWyoOSF\r\n");
        buffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""
                + file.getFileName() + "\"\r\n");
        buffer.append("Content-Type: application/octet-stream\r\n\r\n");
        OutputStream os = new DataOutputStream(con.getOutputStream());
        os.write(buffer.toString().getBytes());
        InputStream is = file.getFileItem().getInputStream();
        byte[] filebuffer = new byte[10240];
        synchronized (filebuffer) {
            int length = -1;
            while (-1 != (length = is.read(filebuffer))) {
                os.write(filebuffer, 0, length);
            }
        }
        os.flush();
        buffer = new StringBuffer();
        buffer.append("\r\n--LamfireFormBoundaryucJiylDzwZWyoOSF\r\n");
        buffer.append("Content-Disposition: form-data; name=\"" + "filedir"
                + "\"\r\n");
        buffer.append("Content-Type: text/plain;charset=" + "UTF-8"
                + "\r\n\r\n");
        buffer.append(dir);
        os.write(buffer.toString().getBytes());

        os.flush();
        os.close();

        is = con.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        con.disconnect();
  • 写回答

2条回答 默认 最新

  • Wismyluckstar 2014-12-02 05:06
    关注

    con.setChunkedStreamingMode(1024 * 1024); 这句话的设置问题吧

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作