背景:做一个溯源秤的系统 做了一个附件的下载到本的秤端系统 称端是springboot ,服务端是nutz(和springboot差不多的国产框架),不多说上代码
客户端代码
/**
* @title: fileToZip
* @author: DXW
* @date: 2019年8月24日 上午 08:41:13
* @description: 从服务端取附件流存到本地
* @param:
* @return:
*/
@ResponseBody
@GetMapping(value = "/fileToZip")
public void fileToZip(String name, HttpServletResponse response) throws IOException {
//服务端附件位置 后期可以不要 从服务端表里查
String param = "F:/webapps/file/one.html/";
//客户端存放附件位置
String zipFilePath = "F:/apache-tomcat-6.0/webapps/SOURCE/";
//服务端取流接口地址 临时
String filePath = "http://192.168.XXX.XXX:XXX/open/netPay/fileToZips?path=F:/apache-tomcat-6.0/webapps/file/one.html/";
InputStream inputStream = sendPost(filePath, param);
String fileName = "ss";
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
if (zipFile.exists()) {
System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 10];
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry("11");
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
int read = 0;
while ((read = inputStream.read(bufs, 0, 1024 * 10)) != -1) {
zos.write(bufs, 0, read);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
// 关闭流
try {
if (null != inputStream)
inputStream.close();
if (null != zos)
zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
/**
* @title: sendPost
* @author: DXW
* @date: 2019年8月24日 上午 08:42:28
* @description: http 调取服务端接口 取附件流
* @param: filePath param
* @return:
*/
public InputStream sendPost(String filePath, String param) throws IOException {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
ByteArrayInputStream inputStream = null;
URL realUrl = new URL(filePath);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
Map mapTypes = JSON.parseObject(result);
String base64String = mapTypes.get("data").toString();
byte[] bytes = new BASE64Decoder().decodeBuffer(base64String);
inputStream = new ByteArrayInputStream(bytes);
return inputStream;
}
服务端代码
/**
* @title: fileToZips
* @author: DXW
* @date: 2019年8月24日 上午 08:29:06
* @description: 取本地附件 以流传输到客户端
* @param: path
* @return:
*/
@At
@Ok("json")
public Result fileToZips(String path) throws IOException {
File file = null;
file = new File(path);
FileInputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream data = new ByteArrayOutputStream();
byte[] by = new byte[1024];
int len = -1;
while ((len = inputStream.read(by)) != -1) {
data.write(by, 0, len);
}
BASE64Encoder encoder = new BASE64Encoder();
String strNetImageToBase64 = encoder.encode(data.toByteArray());
System.out.println(strNetImageToBase64);
return Result.success("",strNetImageToBase64);
}