以下方法为通用版本 实测图片和pdf 都没有问题 上传方法需要前端配合post请求 ,下载前端用a标签就可以,预览 前端使用ifrme标签   ,就可以实现基本功能。。。

1、文件本地上传

public String uploadFile(@RequestParam("file") Part file, @RequestParam(value = "dirPath", required = false) String dirPath) throws IOException {
String fileName = file.getSubmittedFileName();//项目路径
String dir = ConversionFactoryUtil.modulePath(dirPath == null ? "File" : dirPath);
File dateDir = new File(dir);
if (!dateDir.exists()) {
dateDir.mkdirs();
}
String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
String fName = UUID.randomUUID().toString().replace("-", "");
String filePath = File.separator + dir + File.separator + fName + "." + prefix;
InputStream inputStream = file.getInputStream();
Files.copy(inputStream, Paths.get(ConversionFactoryUtil.rootPath() + filePath));
Map<String, String> result = new HashMap<>();
result.put("fileName", fileName);
result.put("filePath", filePath);
return gson.toJson(result);
}

2、文件本地下载

public String downLoadFile(String filePath, HttpServletResponse res, HttpServletRequest request) {
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
try {
res.setContentType("application/doc");
final String userAgent = request.getHeader("USER-AGENT");
if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
fileName = URLEncoder.encode(fileName,"UTF-8");
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
fileName = new String(fileName.getBytes(), "ISO8859-1");
}else{
fileName = URLEncoder.encode(fileName,"UTF-8");//其他浏览器
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 通知浏览器以附件形式下载
res.addHeader("Content-Disposition", "attachment;filename=" +fileName);//这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开 byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os;
try {
os = res.getOutputStream();
File file = new File(ConversionFactoryUtil.rootPath() + File.separator + filePath);
if (file.exists()) {
bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, i);
os.flush();
i = bis.read(buff);
}
return "success";
} else {
System.out.println("file not exists ...filePath:" + filePath);
return "file not local exists ...";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

3、文件本地预览

public String previewFile(String filePath, HttpServletResponse res, HttpServletRequest request) {
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
try {
res.setContentType("application/doc");
final String userAgent = request.getHeader("USER-AGENT");
if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
fileName = URLEncoder.encode(fileName,"UTF-8");
}else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
fileName = new String(fileName.getBytes(), "ISO8859-1");
}else{
fileName = URLEncoder.encode(fileName,"UTF-8");//其他浏览器
}
res.reset();// 非常重要
res.setHeader("Content-Disposition", "inline; filename=" + fileName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
File file = null;
try {
os = res.getOutputStream();
file = new File(ConversionFactoryUtil.rootPath() + filePath);
if (file.exists()) {
bis = new BufferedInputStream(new FileInputStream(file));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, i);
os.flush();
i = bis.read(buff);
}
return "success";
} else {
return "file not local exists ...";
} } catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
os.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

最新文章

  1. javascript的假查询
  2. FB引擎系列-之CloudSand
  3. angularjs input上传图片前获取图片的Size
  4. Nginx 使用 sever 段规则屏蔽恶意 User Agent
  5. LeetCode:Pascal&#39;s Triangle I II
  6. swift初识
  7. php关于static关键字
  8. Jcrop 做图片剪裁 在IE中无法显示问题解决办法
  9. 转 ——eclipse下进行Python开发 环境配置
  10. 用 PS 调整服务器时间
  11. 打造基于Clang LibTooling的iOS自动打点系统CLAS(三)
  12. Numpy的小总结
  13. Java集合框架总结
  14. monaco editor + vue的配置
  15. Egret--设置全屏,控制浏览器全屏
  16. ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)
  17. jeecg多页签的选择切换
  18. android 编译环境 &amp; 开发教程 【持续更新】
  19. [GO]工程管理
  20. 监听浏览器返回,pushState,popstate 事件,window.history对象

热门文章

  1. Android开发之获取系统所有进程信息。
  2. Python_06-函数与模块
  3. ORA-01795: maximum number of expressions in a list is 1000
  4. 在 Ruby 中执行 Shell 命令的 6 种方法
  5. python简单爬虫(一)
  6. Oracle ADF VO排序及VO的查询模式
  7. MongoHelper
  8. Differential expression analysis for paired RNA-seq data 成对RNA-seq数据的差异表达分析
  9. 京东应用架构设计ppt阅读总结
  10. eclipse find 两位数