下载文件系统中的文件
@RequestMapping("/fdfs/downloadQRCode")
public ResponseEntity<byte[]> downloadQRCode(String fileUrl, HttpServletResponse response) throws IOException {
//http://192.168.2.131:8888/group1/M00/00/00/wKgCg10Z2aiAIDHRAAByi9Kp4qU348.png
//http://192.168.2.131:8888/ 是文件系统域名
//group 是 group1 path是 :/M00/00/00/wKgCg10Z2aiAIDHRAAByi9Kp4qU348.png
String fileSubstringUrl = fileUrl.replace(CommonConstant.FDFS_URL,"");
String group = fileSubstringUrl.substring(0, fileSubstringUrl.indexOf("/"));
//获取文件的路径
String path = fileSubstringUrl.substring(fileSubstringUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
String fileSubstringUrl = fileUrl.replace(CommonConstant.FDFS_URL,"");
String group = fileSubstringUrl.substring(0, fileSubstringUrl.indexOf("/"));
String path = fileSubstringUrl.substring(fileSubstringUrl.indexOf("/") + 1);
// 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式
String fileName = System.currentTimeMillis()+"";
response.setHeader("Content-disposition", "attachment;filename="+ fileName + URLEncoder.encode(path, "UTF-8"));
response.setCharacterEncoding("UTF-8");
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ResponseEntity<byte[]> entity = null;
entity = new ResponseEntity<byte[]>(bytes, HttpStatus.OK);
return entity;
}
如果是通过feign层进行下载
那么需要写返回值 如上面所示 返回值类型为:ResponseEntity<byte[]>
如果不通过feign层 那么不需要返回值即:void