java代码:
Map map = new HashMap(); String fileStr = getFileStr(path);//这里path就是我们服务器上的文件存放路径 map.put("appFileStr", fileStr); String result = HttpClientUtil.callHttpPost(ip, port, "reciveFile", map); public String getFileStr(String path) {//这里是我们封装好的读取文件获得的并经过BASE64加密之后的字符串的方法 String resultStr = ""; try { int b; File file = new File(path); byte[] temp = new byte[(int) (file.length())]; FileInputStream inputstream = new FileInputStream(file);//本地文件的输入流 BufferedInputStream in = new BufferedInputStream(inputstream); StringBuffer fileStr = new StringBuffer(); BASE64Encoder encoder = new BASE64Encoder(); while ((b = in.read(temp)) != -1) { fileStr.append(encoder.encode(temp)); } inputstream.close(); resultStr = fileStr.toString(); } catch (IOException e) { e.printStackTrace(); } return resultStr; }
Node.js客户端代码:
/** * Created by Administrator on 2019/12/23. *指尖敲打着世界 ----一个阳光而又不失帅气的少年!!!. */ var express=require('express'); /*引入*/ var bodyParser = require('body-parser'); var fs=require("fs"); var app=new express(); /*实例化*/ //配置body-parser中间件 // parse application/x-www-form-urlencoded//处理表单数据 app.use(bodyParser.urlencoded({ extended: false })) // parse application/json //处理json数据
app.use(bodyParser.json({limit : "2100000kb"}));
app.post('/reciveFile',function(req,res){ var app = new Buffer.from(req.body.appFileStr, 'base64'); fs.writeFileSync("D:/upload/writeFileTest.zip",app);//注意:writeFile写入的文件zip打不开,具体原因不明,同道高人如果知晓,希望能够指点一二 res.send({retMsg: '收到发送文件流请求,正在接收', retCode: "1"}); }) app.listen(8089);
附录HttpClientUtil代码:
package com.yxjr.common.utils; import net.sf.json.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.UnsupportedEncodingException; import java.util.Map; public class HttpClientUtil { //post提交调用方法 public static String callHttpPost(String hostIP, int port, String actionName, Map<String,String> paramsMap) throws UnsupportedEncodingException { JSONObject resultJSon = new JSONObject(); String callbackResult = ""; System.err.println("请求参数是: IP" + hostIP + " actionName:" + actionName + " params:" + paramsMap); HttpClient httpClient = new DefaultHttpClient(); StringBuilder url = new StringBuilder("http://"); if (hostIP != null && !"".equals(hostIP) && !"".equals(actionName) && actionName != null) { url.append(hostIP).append(":").append(String.valueOf(port)).append("/").append(actionName); } // if (hostIP != null && !"".equals(hostIP) && !"".equals(actionName) && actionName != null) { // url.append(hostIP).append(":").append(String.valueOf(port)).append("/") // .append(baseUrl).append("/").append(actionName); // } System.out.println(url); String str =""; if(paramsMap!=null && paramsMap.size()!=0){ str = JSONObject.fromObject(paramsMap).toString(); } System.err.println("请求参数为:"+ str); HttpPost httpPost = new HttpPost(url.toString()); StringEntity entity = new StringEntity(str, "utf-8"); //entity.setContentEncoding("utf-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); try { HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); String entityString = EntityUtils.toString(response.getEntity()); System.err.println("请求返回码为:"+ String.valueOf(statusCode)+" 返回结果为:" +entityString); if(statusCode==200 ){ callbackResult = entityString; }else{ resultJSon.put("retCode",-1); resultJSon.put("retMsg","请求异常,请检查请求内容"); resultJSon.put("resultCode",statusCode); callbackResult = resultJSon.toString(); } } catch (Exception e) { System.err.println("客户端连接异常:"+e.getMessage()); resultJSon.put("retCode",-2); resultJSon.put("retMsg","连接异常,请检查客户端机服务是否正常开启"); resultJSon.put("resultCode",500); callbackResult = resultJSon.toString(); e.printStackTrace(); } return callbackResult; } }