Java后端HttpClient Post提交文件流 及服务端接收文件流
客户端将文件转换为流发送:
依赖的包:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.4</version>
</dependency>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <br> import com.alibaba.fastjson.JSONObject;<br> import java.io.*;<br> import java.net.HttpURLConnection;<br> import java.net.URL;<br> public static void main(String[] args) throws IOException { DataInputStream in = null ; OutputStream out = null ; HttpURLConnection conn = null ; JSONObject resposeTxt = null ; InputStream ins = null ; ByteArrayOutputStream outStream = null ; try { // URL url = new URL("http://192.168.3.11:8081/mes-boot-doc/test/fileupload?fileName=shafei.xls"); URL url = new URL( "http://localhost:8081/mes-boot-doc/test/fileupload?fileName=shafei.xls" ); conn = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput( true ); conn.setUseCaches( false ); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Content-Type" , "text/html" ); conn.setRequestProperty( "Cache-Control" , "no-cache" ); conn.setRequestProperty( "Charsert" , "UTF-8" ); conn.connect(); conn.setConnectTimeout( 10000 ); out = conn.getOutputStream(); File file = new File( "C:/Users/Dell/Desktop/print/shafei.xls" ); in = new DataInputStream( new FileInputStream(file)); int bytes = 0 ; byte [] buffer = new byte [ 1024 ]; while ((bytes = in.read(buffer)) != - 1 ) { out.write(buffer, 0 , bytes); } out.flush(); // 返回流 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { ins = conn.getInputStream(); outStream = new ByteArrayOutputStream(); byte [] data = new byte [ 1024 ]; int count = - 1 ; while ((count = ins.read(data, 0 , 1024 )) != - 1 ) { outStream.write(data, 0 , count); } data = null ; resposeTxt = JSONObject.parseObject( new String(outStream .toByteArray(), "UTF-8" )); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null ) { in.close(); } if (out != null ) { out.close(); } if (ins != null ) { ins.close(); } if (outStream != null ) { outStream.close(); } if (conn != null ) { conn.disconnect(); } } } |
服务端接收文件流生成文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | @PostMapping ( "/fileupload" ) public String uploadFile(HttpServletRequest request,HttpServletResponse response) throws Exception{ String fileName = request.getParameter( "fileName" ); log.info( "filename:" +fileName); // String fileName ="shafei.xls"; // String fileFullPath = "C:/Users/Dell/Desktop/print/test/" + fileName; String fileFullPath = "/root/uploadfile/apache-tomcat-8.5.42/" + fileName; InputStream input = null ; FileOutputStream fos = null ; try { input = request.getInputStream(); File file = new File( "/root/uploadfile/apache-tomcat-8.5.42/" ); if (!file.exists()){ file.mkdirs(); } fos = new FileOutputStream(fileFullPath); int size = 0 ; byte [] buffer = new byte [ 1024 ]; while ((size = input.read(buffer, 0 , 1024 )) != - 1 ) { fos.write(buffer, 0 , size); } //响应信息 json字符串格式 Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put( "flag" , true ); //生成响应的json字符串 String jsonResponse = JSONObject.toJSONString(responseMap); sendResponse(jsonResponse,response); } catch (IOException e) { //响应信息 json字符串格式 Map<String,Object> responseMap = new HashMap<String,Object>(); responseMap.put( "flag" , false ); responseMap.put( "errorMsg" , e.getMessage()); String jsonResponse = JSONObject.toJSONString(responseMap); sendResponse(jsonResponse,response); } finally { if (input != null ){ input.close(); } if (fos != null ){ fos.close(); } } return null ; } /** * 返回响应 * * @throws Exception */ private void sendResponse(String responseString,HttpServletResponse response) throws Exception { response.setContentType( "application/json;charset=UTF-8" ); PrintWriter pw = null ; try { pw = response.getWriter(); pw.write(responseString); pw.flush(); } finally { IOUtils.closeQuietly(pw); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步