Java(HttpURLConnection)内置HTTP模块上传文件

Java自带HTTP模块上传文件

        URL url = null;
        HttpURLConnection httpConn = null;
        BufferedReader reader = null;
        String viewUrl = "";
        try {
            String BOUNDARY = UUID.randomUUID().toString().replace("-", "");
            url = new URL(dcsConfig.getServerDomain() + dcsServerUrl);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setConnectTimeout(10000); // 设置发起连接的等待时间,10s
            httpConn.setReadTimeout(300000); // 设置数据读取超时的时间,300s
            httpConn.setUseCaches(false); // 设置不使用缓存
            httpConn.setDoOutput(true);
            httpConn.setRequestMethod("POST");

            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
            httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            OutputStream os = httpConn.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);

            StringBuffer strBuf = new StringBuffer();
            strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
            strBuf.append("Content-Disposition: form-data; name=\"convertType\"").append("\r\n\r\n"); //转换类型
            strBuf.append("类型");
            strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
            strBuf.append("Content-Disposition: form-data; name=\"isDelSr\"").append("\r\n\r\n"); //是否删除源文件:0=删除,1=不删除
            strBuf.append("0");
            strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
            strBuf.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n");
            strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
            bos.write(strBuf.toString().getBytes());

            // 开始写出文件的二进制数据
            DataInputStream in = new DataInputStream(file.getInputStream());
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in.read(bufferOut)) != -1) {
                bos.write(bufferOut, 0, bytes);
            }
            bos.write(("\r\n--" + BOUNDARY).getBytes());
            in.close();
            bos.flush();
            bos.close();
            os.close();

            // 读取返回数据
            StringBuffer strRet = new StringBuffer();
            reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                strRet.append(line);
            }
            reader.close();
            JSONObject json = JSON.parseObject(strRet.toString());
            
        } catch (Exception ex) {
            logger.error("-{}", ex);
        } finally {
            if (reader != null) {
                reader = null;
            }
            httpConn.disconnect();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值