• JAVA用post请求上传二进制文件


    /**
    	 * 多文件上传的方法
    	 * 
    	 * @param actionUrl:上传的路径
    	 * @param uploadFilePaths:需要上传的文件路径,数组
    	 * @return
    	 */
    	@SuppressWarnings("finally")
    	public static String uploadFile(String actionUrl, String[] uploadFilePaths) {
    		String end = "
    ";
    		String twoHyphens = "--";
    		String boundary = "*****";
    		
    		DataOutputStream ds = null;
    		InputStream inputStream = null;
    		InputStreamReader inputStreamReader = null;
    		BufferedReader reader = null;
    		StringBuffer resultBuffer = new StringBuffer();
    		String tempLine = null;
    		
    		 try {
    			// 统一资源
    			 URL url = new URL(actionUrl);
    			// 连接类的父类,抽象类
    			 URLConnection urlConnection = url.openConnection();
    			// http的连接类
    			 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
    			// 设置是否从httpUrlConnection读入,默认情况下是true;
    			 httpURLConnection.setDoInput(true);
    			// 设置是否向httpUrlConnection输出
    			 httpURLConnection.setDoOutput(true);
    			// Post 请求不能使用缓存
    			 httpURLConnection.setUseCaches(false);
    			// 设定请求的方法,默认是GET
    			 httpURLConnection.setRequestMethod("POST");
    			// 设置字符编码连接参数
    			 httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
    			// 设置字符编码
    			 httpURLConnection.setRequestProperty("Charset", "UTF-8");
    			// 设置请求内容类型
    			 httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    			 
    			// 设置DataOutputStream
    			 ds = new DataOutputStream(httpURLConnection.getOutputStream());
    			 for (int i = 0; i < uploadFilePaths.length; i++) {
    				 String uploadFile = uploadFilePaths[i];
    				 String filename = uploadFile.substring(uploadFile.lastIndexOf("\") + 1);
    				 ds.writeBytes(twoHyphens + boundary + end);
    				 ds.writeBytes("Content-Disposition: form-data; " + "name="file" + i + "";filename="" + filename
    						 + """ + end);
    				 ds.writeBytes(end); 
    				 FileInputStream fStream = new FileInputStream(uploadFile);
    				 int bufferSize = 1024;
    				 byte[] buffer = new byte[bufferSize];
    				 int length = -1;
    				 while ((length = fStream.read(buffer)) != -1) {
    					 ds.write(buffer, 0, length);
    				 }
    				 ds.writeBytes(end);
    				 /* close streams */
    				 fStream.close();
    			 }
    			 ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
    			 /* close streams */
    			 ds.flush();
    			 if (httpURLConnection.getResponseCode() >= 300) {
    				 throw new Exception(
    						 "HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
    			 }
    			 if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
    				 inputStream = httpURLConnection.getInputStream();
    				 inputStreamReader = new InputStreamReader(inputStream);
    				 reader = new BufferedReader(inputStreamReader);
    				 tempLine = null;
    				 resultBuffer = new StringBuffer();
    				 while ((tempLine = reader.readLine()) != null) {
    					 resultBuffer.append(tempLine);
    					 resultBuffer.append("
    ");
    				 }
    			 }
    		 }catch(Exception e) {
    			 e.printStackTrace();
    		 }finally {
    			 if (ds != null) {
    				 try {
    					 ds.close();
    				 } catch (IOException e) {
    					// TODO Auto-generated catch block
    					 e.printStackTrace();
    				 }
    			 }
    			 if (reader != null) {
    				 try {
    					 reader.close();
    				 } catch (IOException e) {
    					// TODO Auto-generated catch block
    					 e.printStackTrace();
    				 }
    			 }
    			 if (inputStreamReader != null) {
    				 try {
    					 inputStreamReader.close();
    				 } catch (IOException e) {
    					// TODO Auto-generated catch block
    					 e.printStackTrace();
    				 }
    			 }
    			 if (inputStream != null) {
    				 try {
    					 inputStream.close();
    				 } catch (IOException e) {
    					// TODO Auto-generated catch block
    					 e.printStackTrace();
    				 }
    			 }
    			 return resultBuffer.toString();
    		 }
    	}
    

     转自:【JAVA】通过HttpURLConnection 上传和下载文件(二) - H__D - 博客园 (cnblogs.com)

  • 相关阅读:
    优秀的云架构师需要学什么技能
    dkh人力资源大数据解决方案整体架构
    大数据hadoop与spark的区别
    hadoop技术入门学习之发行版选择
    大数据开发基础知识需要掌握哪些
    智慧人社政务云平台建设方案架构案例介绍
    [项目机会]citrix 虚拟桌面对于java等高CPU占用率如何解决
    [办公自动化]无法使用江南天安usbkey 无法使用视频网站
    [学习笔记]从0到1
    [办公自动化]目录修改以及插入分页符后行间距自动变宽
  • 原文地址:https://www.cnblogs.com/wwssgg/p/15502229.html
Copyright © 2020-2023  润新知