以下方法为通用版本 实测图片和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. Mac 下输入特殊字符

    [Mac 下输入特殊字符] 快捷键是 Control + Commans + Space

  2. NBU 还原windows ORACLE数据库(EC)

    rman target / startup nomount; run{ allocate channel ch00 type 'SBT_TAPE'; send 'nb_ora_serv=nbumast ...

  3. JS 将json数组转为嵌套层级数组

    ele UI 的树级菜单的数据要求是这种嵌套的,但是Ztree的老用发的是 var zNodes =[ { id:, pId:, name:"zTree Home", pid:0} ...

  4. 307. Range Sum Query - Mutable查询求和的范围(可变)

    [抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  5. Linux运维实战之DNS(bind)服务器的安装与配置

    转自http://sweetpotato.blog.51cto.com/533893/1598225 上次博文我们讨论了DNS的基础,本次博文我们重点来看看如何配置一台DNS服务器. [本次博文的主要 ...

  6. jquery记录

    jquery validate验证框架 参考:http://www.cnblogs.com/linjiqin/p/3431835.html http://www.runoob.com/jquery/j ...

  7. iPhone X的UI设计技巧

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 果粉们翘首以待的iPhone X终于开始预售了!同样满怀期待的还有设计师和开发人员,他们将在iPho ...

  8. abp CrudAppService 自定义分页、排序

    public class GetAllTasksInput : PagedAndSortedResultRequestDto { public TaskState? State { get; set; ...

  9. c# 调用外包程序 等待处理完成结果

    string root = @"J:\yaoqianshu"; string pythonPath = "解压缩拷贝启动动画测试(新).py"; string ...

  10. Nginx学习基础(一)

    Nginx是个可靠高效的中间件,就是跟其他语言连接,可以做为一个工具的服务器. 可以处理的问题: 1.反向代理 (1)正向代理(以客户端为主):访问网站的时候,早起是在做通过n多个路由访问网站的操作, ...