SpringBoot上传下载
程序员文章站
2022-04-30 22:25:56
...
额外加入的依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
上传
上传页面:注意上传域的name属性要和controller参数名一致,enctype属性
<form method="POST" enctype="multipart/form-data" action="fileUpLoad.action">
<p>
文件:<input type="file" name="file" />
</p>
<p>
<input type="submit" value="上传" />
</p>
</form>
上传参数控制
package com.example.demo.config;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置上传
* @author 柴火
*
*/
@Configuration
public class FileUpLoadConfig {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//// 设置文件大小限制,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
factory.setMaxFileSize("128KB"); // KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("256KB");
// Sets the directory location where files will be stored. 设置上传路径
//factory.setLocation("路径地址");
return factory.createMultipartConfig();
}
}
controller代码(2种上传方式)
package com.example.demo.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UpLoadController {
@RequestMapping("/fileUpLoad.action")
@ResponseBody
public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
BufferedOutputStream stream = null;
if (!file.isEmpty()) {
String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
File newFile=new File(path+"/"+file.getOriginalFilename());
if(!newFile.exists()){
newFile.createNewFile();
}
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
out.write(file.getBytes());
out.flush();
out.close();
return "上传成功";
}
return "上传失败";
}
}
package com.example.demo.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UpLoadController {
@RequestMapping("/fileUpLoad.action")
@ResponseBody
public String upLoadFile(MultipartFile file) throws IllegalStateException, IOException {
BufferedOutputStream stream = null;
if (!file.isEmpty()) {
// 打印文件的名称
System.out.println("FileName:" + file.getOriginalFilename());
// 确定上传文件的位置
String path = "E:/stsWorkSpace/SpringBoot_Test/src/main/resources/upload";
File newFile = new File(path, file.getOriginalFilename());
//如果不存在,创建一哥副本
if (!newFile.exists()) {
newFile.createNewFile();
}
//将io上传到副本中
file.transferTo(newFile);
return "上传成功";
}
return "上传失败";
}
}
下载
package com.example.demo.controller;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DownLoadController {
@RequestMapping("/fileDownLoad.action")
public ResponseEntity<byte[]> textFileDownload() throws Exception {
String path = "E:/WorkSpace1/SpringMVC_001/WebContent/WEB-INF/fileupload/1.jpg";
File file = new File(path);
HttpHeaders headers = new HttpHeaders();
// String fileName=new
// String("你好.xlsx".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
String fileName = new String(file.getName().getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}
}
上一篇: CDR手绘卡通温馨的幸福小苑住宅图教程
下一篇: CDR怎么设计一款手提袋包装?
推荐阅读
-
springboot+layUI可视化旅游网站 旅游管理系统 开发分享
-
springboot+vue.js智能停车场管理系统
-
ssm框架下的网上书城项目的开发-springboot下开发(使用jsp)
-
springboot医院综合管理系统 医院门诊管理系统 5年老码农分享开发经验
-
springboot+vue.js仓储调度平台 仓库管理系统 开发纪实
-
以[SpringBoot房产平台]为例算法精进之路系列1
-
springboot客户关系管理系统 CRM 记录一次开发过程
-
springboot旅游推荐系统 算法解析篇
-
全网最好的SpringBoot全终端H5垃圾分类与回收公益平台 垃圾分类推广系统 记一次软件开发实录
-
一篇文章带你入门Springboot整合微信登录与微信支付(附源码)