栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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("rn").append("--").append(BOUNDARY).append("rn");
            strBuf.append("Content-Disposition: form-data; name="convertType"").append("rnrn"); //转换类型
            strBuf.append("类型");
            strBuf.append("rn").append("--").append(BOUNDARY).append("rn");
            strBuf.append("Content-Disposition: form-data; name="isDelSr"").append("rnrn"); //是否删除源文件:0=删除,1=不删除
            strBuf.append("0");
            strBuf.append("rn").append("--").append(BOUNDARY).append("rn");
            strBuf.append("Content-Disposition: form-data; name="file"; filename="" + filename + ""rn");
            strBuf.append("Content-Type:" + contentType + "rnrn");
            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(("rn--" + 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();
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397259.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号