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); 这句话的设置问题吧

    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求