|
@@ -286,6 +286,70 @@ public class CommonController {
|
|
|
//
|
|
|
// }
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取文件
|
|
|
+ * 请求地址:http://localhost:8080/common/downloadFile/{file/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ @GetMapping(value = "/downloadFile/**")
|
|
|
+ public void downloadFile(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ // ISO-8859-1 ==> UTF-8 进行编码转换
|
|
|
+ String filePath = extractPathFromPattern(request);
|
|
|
+ // 其余处理略
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ filePath = filePath.replace("..", "");
|
|
|
+ if (filePath.endsWith(",")) {
|
|
|
+ filePath = filePath.substring(0, filePath.length() - 1);
|
|
|
+ }
|
|
|
+ String localPath = uploadpath;
|
|
|
+ String fileurl = localPath + File.separator + filePath;
|
|
|
+ String fileName = fileurl.substring(fileurl.lastIndexOf("/")+1);
|
|
|
+ //浏览器设置,处理下载文件时文件名乱码
|
|
|
+ String userAgent = request.getHeader("User-Agent");
|
|
|
+ if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
|
|
|
+ //IE浏览器处理
|
|
|
+ fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
|
|
|
+ } else {
|
|
|
+ // 非IE浏览器的处理:
|
|
|
+ fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
|
|
+ }
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("content-type", "application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + fileName );// 设置文件名
|
|
|
+ inputStream = new BufferedInputStream(new FileInputStream(fileurl));
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buf)) > 0) {
|
|
|
+ outputStream.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ response.flushBuffer();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("预览图片失败" + e.getMessage());
|
|
|
+ // e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @功能:pdf预览Iframe
|
|
|
* @param modelAndView
|