以下方法为通用版本 实测图片和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;
}

Java 文件本地上传、下载和预览的实现的更多相关文章

  1. java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。

    java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...

  2. java文件夹上传下载控件分享

    用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个需要在JAVA.MyEclipse环境下大文件上传的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助. 以下 ...

  3. java文件断点续传上传下载解决方案

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  4. java文件夹上传下载组件

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  5. JAVA 文件的上传下载

    一.上传文件 1.使用 transferTo 上传 @ResponseBody @RequestMapping(value = "/file/upload") public Res ...

  6. jsp+springmvc实现文件上传、图片上传和及时预览图片

    1.多文件上传:http://blog.csdn.net/a1314517love/article/details/24183273 2.单文件上传的简单示例:http://blog.csdn.net ...

  7. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  8. Spring实现文件的上传下载

    背景:之前一直做的是数据库的增删改查工作,对于文件的上传下载比较排斥,今天研究了下具体的实现,发现其实是很简单.此处不仅要实现单文件的上传,还要实现多文件的上传. 单文件的下载知道了,多文件的下载呢? ...

  9. SocketIo+SpringMvc实现文件的上传下载

    SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...

  10. JAVAWEB之文件的上传下载

    文件上传下载 文件上传: 本篇文章使用的文件上传的例子使用的都是原生技术,servelt+jdbc+fileupload插件,这也是笔者的习惯,当接触到某些从未接触过的东西时,总是喜欢用最原始的东西将 ...

随机推荐

  1. SublimeText Videos Notes

    [SublimeText Videos Notes] Getting Started 1.Hello:https://tutsplus.com/course/improve-workflow-in-s ...

  2. 数据库 alert.log 日志中出现 "[Oracle][ODBC SQL Server Wire Protocol driver][SQL Server] 'RECOVER'"报错信息

    现象描述: (1).数据库通过调用透明网络实现分布式事务,但透明网关停用后,失败的分布式事务并未清理. (2).数据库 alert 日志 Thu Sep 06 06:53:00 2018 Errors ...

  3. 关于mybatis框架的总结【转载】

    原文地址:https://www.cnblogs.com/xiaotie666/p/LiujinMybatisSummary.html 此文为转载.请支持原作者. 最近在学习MyBatis框架,我在这 ...

  4. Python3 sorted() 函数

    Python3 sorted() 函数  Python3 内置函数 描述 sorted() 函数对所有可迭代的对象进行排序操作. sort 与 sorted 区别: sort 是应用在 list 上的 ...

  5. 创建和运行Java项目

    ---------siwuxie095                     首先在左侧的工程管理面板 Package Explorer 中,右键->New->Java Project ...

  6. Shrio03 Authenticator、配置多个Realm、SecurityManager认证策略

    1 Authenticator 简介 1.1 层次结构图 1.2 作用 职责是验证用户帐号,是ShiroAPI中身份验证核心的入口点:接口中声明的authenticate方法就是用来实现认证逻辑的. ...

  7. 31-字符串转为 url 格式的两种不同情况

    将此字符串转为 url 格式的: # 如果是转化对象用:data=urllib.parse.urlencode(values) # 如果是转化字符串:s=urllib.parse.quote(s)

  8. mybatis使用原始Dao开发中存在的问题

    1.Dao方法存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的送数据库操作方法. 2.调用SqlSession的数据库需要制定statement ...

  9. 15 Independent Alleles

    Problem Figure 2. The probability of each outcome for the sum of the values on two rolled dice (blac ...

  10. windows server2012部署apache项目访问后台管理系统时tomcat就停了是怎么回事

    是由于环境变量没有配好的原因,找不到jre目录 tomcat的运行需要JRE,一般启动闪退都是因为找不到JRE,也就是说环境安装JDK时环境变量没有配置好. 我们首先打开”命令提示符“窗口,输入jav ...