SpringMVC中使用 MultipartFile 进行文件上传下载及删除
一:引入必要的包
<!--文件上传-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
二:在spring-service中配置
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
三:文件上传
3.1 实现方法
/**
* 文件上传
* @param file
* @param request
* @return
*/
public JSONObject uploadFile(MultipartFile file, HttpServletRequest request){
//获取文件名称
String fileName = file.getOriginalFilename();
//获取文件存放路径
String path = request.getSession().getServletContext().getRealPath("").concat("/uploadFile");
// String path = getUploadPath(fileName,request);
//上传文件
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
return resultErrorJson(fileName);
}
logger.info(fileName+"文件上传成功");
JSONObject jsonObject = new JSONObject();
jsonObject.put("key","true");
jsonObject.put("name",fileName);
jsonObject.put("path",path+"/"+fileName);
return jsonObject;
}
3.2 controller层
@RequestMapping(value="/upload")
@ResponseBody
public JSONObject upload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request){
fileOperation = new FileOperation();
//返回文件后的文件路径
return fileOperation.uploadFile(file,request);
}
3.3 文本递交的时候 注意提交格式
<form action="/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
四:文件下载
/**
* 下载文件
* @param fileName 文件模板
* HttpServletRequest request,
* @param response response请求
*/
public boolean download(String fileName, HttpServletRequest request,HttpServletResponse response) {
try {
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment;fileName="
+ fileName);
String path = request.getSession().getServletContext().getRealPath("")+"/uploadFile/"+fileName;
File tempFile =new File(path); InputStream inputStream = new FileInputStream(tempFile);
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.flush();
os.close();
inputStream.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件下载失败");
return false;
}catch (IOException e) {
e.printStackTrace();
System.out.println("文件下载失败");
return false; }
return true;
}
五 删除文件
/**
* 通过文件绝对路径 删除单个文件
* @param filePath
*/
public boolean delFile(String filePath) {
File delFile = new File(filePath);
if(delFile.isFile() && delFile.exists()) {
delFile.delete();
logger.info("删除文件成功");
return true;
}else {
logger.info("没有该文件,删除失败");
return false;
}
}
SpringMVC中使用 MultipartFile 进行文件上传下载及删除的更多相关文章
- 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载
摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...
- Java 客户端操作 FastDFS 实现文件上传下载替换删除
FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...
- SpringMVC整合fastdfs-client-java实现web文件上传下载
原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...
- SpringMVC ajax技术无刷新文件上传下载删除示例
参考 Spring MVC中上传文件实例 SpringMVC结合ajaxfileupload.js实现ajax无刷新文件上传 Spring MVC 文件上传下载 (FileOperateUtil.ja ...
- SpringMVC(三) RESTful架构和文件上传下载
RESTful架构 REST全名为:Representational State Transfer.资源表现层状态转化.是目前最流行的一种互联网软件架构. 它结构清晰.符合标准.易于理解.扩展方便,所 ...
- minio实现文件上传下载和删除功能
https://blog.csdn.net/tc979907461/article/details/106673570?utm_medium=distribute.pc_relevant_t0.non ...
- SpringMVC中使用CommonsMultipartResolver进行文件上传
概述: CommonsMultipartResolver是基于Apache的Commons FileUpload来实现文件上传功能的.所以在项目中需要相应的jar文件. FileUpload版本要求1 ...
- SpringMVC学习笔记八:文件上传下载(转)
转自:http://www.cnblogs.com/WJ-163/p/6269409.html 一.关键步骤 ①引入核心JAR文件 SpringMVC实现文件上传,需要再添加两个jar包.一个是文件上 ...
- java中io流实现文件上传下载
新建io.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
随机推荐
- 使用GRPC远程服务调用
远程过程调用(英语:Remote Procedure Call,缩写为 RPC)是一个计算机通信协议.该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程.如 ...
- (办公)mysql连接不上(java.sql.SQLException: null, message from server: "Host 'LAPTOP-O0GA2P8J' is not allowed to connect to this MySQL server")(转)
转载自csdn文章:https://blog.csdn.net/Tangerine_bisto/article/details/803461511.对所有主机进行访问授权 GRANT ALL PRIV ...
- NSTimer 不工作 不调用方法
比如,定义一个NSTimer来隔一会调用某个方法,但这时你在拖动textVIew不放手,主线程就被占用了.timer的监听方法就不调用,直到你松手,这时把timer加到 runloop里,就相当于告诉 ...
- 小小白搭建nextcloud云盘
我是一名linux的小小白,今天就利用自己的所学搭建属于自己的云盘——nextcloud. 本人学生狗,普通的云盘也要几十块钱,既然我们只是拿来搭建巩固自己知识并不做为生产力,我们就用VMware W ...
- Bootstrap -- 表格样式、表单布局
Bootstrap -- 表格样式.表单布局 1. 表格的一些样式 举例: <!DOCTYPE html> <html> <head> <meta http- ...
- yum自动安装mysql
1.安装客户端和服务器端查看CentOS自带mysql: yum list installed | grep mysql卸载CentOS系统自带mysql数据库? yum -y remove mysq ...
- 【English】20190415
approximately大约 [əˈprɑ:ksɪmətli] This install will take + minutes and requires the download of appro ...
- ZooKeeper学习总结 第一篇:ZooKeeper快速入门
1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 它有如下的一些特点: 简单 Zookeeper的核 ...
- C# — 调用dll出现试图加载不正确格式的程序问题
今天在调用百度dll包时,运行项目出现了如下警告: 修改:鼠标右击项目名称----选择属性----生成-----平台目标-----X64(由于我调用的是X64的dll包,所以这里选择X64,网上许多说 ...
- L2-4 部落 (25 分)
在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈.我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同 ...