springBoot实现⽂件上传与下载1. ⽂件下载
准备⼀个⽂件C:/Users/admin/Desktop/test.xlsx
springboot原理图解/**
* 定制分析下载模板⽂档
* @param response
* @return
*/
@RequestMapping("/downLoadTemplateExcel")
public Info downLoadTemplateExcel(HttpServletResponse response){
logger.info("/customAnalysisConfig/downLoadTemplateExcel");
Info infos=new Info();
/
/ 创建输⼊输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//String url = "C:/Users/admin/Desktop/test.xlsx";
String url =null;
if(environment.equals("local")){//本地⽂件路径
url ="C:/Users/admin/Desktop/test.xlsx";
}else {//服务器⽂件路径
url = "/usr/java/test.xlsx";
}
String downLoadPath = url;
String fileName="template.xlsx";//⽣成的⽂件名
File file2 = new File(downLoadPath);//要下载的⽂件对象
if (!ists()) {//如果⽬录不存在,创建⽬录
file2.mkdirs();
}
long fileLength = file2.length();// 获取⽂件长度
try {
//Content-Disposition: attachment; filename="filename.xls"
//第⼀个参数是attachment(意味着消息体应该被下载到本地;⼤多数浏览器会呈现⼀个“保存为”的对话框,
// 将filename的值预填为下载后的⽂件名,假如它存在的话)
response.setHeader("Content-disposition", "attachment; filename=" + new Bytes("utf-8"), "ISO8859-1"));
//Content-Type 实体头部⽤于指⽰资源的MIME类型 media type
response.setHeader("Content-Type", "application/json");
//Content-Length, HTTP消息长度, ⽤⼗进制数字表⽰的⼋位字节的数⽬
response.setHeader("Content-Length", String.valueOf(fileLength));
// 创建输⼊输出流实例
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new OutputStream());
// 创建字节缓冲⼤⼩
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = ad(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bis != null)
try {
bis.close();// 关闭输⼊流
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();// 关闭输出流
} catch (IOException e) {
e.printStackTrace();
}
}
infos.setCode("1");
infos.setMsg("成功");
return infos ;
}
2. 单⽂件上传
准备⼀个upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="localhost:4000/pdClientManagerSystem/customAnalysisConfig/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" value="Select your file: ">
<input type="submit" value="Upload">
</form>
</body>
</html>
注意这⾥的 name="file"
//单⽂件上传的功能
@RequestMapping(value = "/upload")
public String upload(MultipartFile file) {
try {
if (file.isEmpty()) {
return "file is empty";
}
String fileName = OriginalFilename();//获取⽂件名
String suffixName = fileName.substring(fileName.lastIndexOf("."));//切割⽂件名
log.info("上传的⽂件名为:" + fileName + " 后缀名为" + suffixName);
// 设置⽂件存储路径(D盘),你可以存放在你想要指定的路径⾥⾯。
String filePath = "D:/upload";
//⽂件存放路径=filePath+/+ fileName
String path = filePath + "/" + fileName;
File dest = new File(path);
// 检测是否存在⽬录
if (!ParentFile().exists()) {
}
return "upload success";
} catch (Exception e) {
e.printStackTrace();
}
return "upload failure";
}
3. 多⽂件上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>批量⽂件上传</title>
</head>
<body>
<form method="post" action="/batch" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
java代码
//多⽂件上传的功能
@RequestMapping("/batch")
public String handleFileUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = (i);
String filePath = "D:/uploads";
if (!file.isEmpty()) {
try {
byte[] bytes = Bytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(filePath  + "/" + OriginalFilename())));
stream.write(bytes);// 写⼊
stream.close();
} catch (Exception e) {
stream = null;
return "the " + i + " file upload failure";
}
} else {
return "the " + i + " file is empty";
}
}
return "upload Multifile success";
}
/**
* 定制分析下载模板⽂档
* @param response
* @return
*/
@RequestMapping("/downLoadTemplateExcel")
public Info downLoadTemplateExcel(HttpServletResponse response){
logger.info("/customAnalysisConfig/downLoadTemplateExcel");
Info infos=new Info();
// 创建输⼊输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//String url = "C:/Users/admin/Desktop/test.xlsx";
String url =null;
if(environment.equals("local")){//本地⽂件路径
url ="C:/Users/admin/Desktop/test.xlsx";
}else {//服务器⽂件路径
url = "/usr/java/test.xlsx";
}
String downLoadPath = url;
String fileName="template.xlsx";//⽣成的⽂件名
File file2 = new File(downLoadPath);//要下载的⽂件对象
if (!ists()) {//如果⽬录不存在,创建⽬录
file2.mkdirs();
}
long fileLength = file2.length();// 获取⽂件长度
try {
//Content-Disposition: attachment; filename="filename.xls"
//第⼀个参数是attachment(意味着消息体应该被下载到本地;⼤多数浏览器会呈现⼀个“保存为”的对话框,// 将filename的值预填为下载后的⽂件名,假如它存在的话)
response.setHeader("Content-disposition", "attachment; filename=" + new Bytes("utf-8"), "ISO8859-1"));
//Content-Type 实体头部⽤于指⽰资源的MIME类型 media type
response.setHeader("Content-Type", "application/json");
//Content-Length, HTTP消息长度, ⽤⼗进制数字表⽰的⼋位字节的数⽬
response.setHeader("Content-Length", String.valueOf(fileLength));
// 创建输⼊输出流实例
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new OutputStream());
// 创建字节缓冲⼤⼩
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = ad(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bis != null)
try {
bis.close();// 关闭输⼊流
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();// 关闭输出流
} catch (IOException e) {
e.printStackTrace();
}
}
infos.setCode("1");
infos.setMsg("成功");
return infos ;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。