springMVC上传和下载文件

这篇博客介绍了如何在SpringMVC框架下实现文件的上传和下载功能,包括从本地存储到FTP服务器的文件上传参考,以及如何通过控制器进行文件下载的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//springMVC只能上传到本地,不同电脑之间参考FTP上传

@RequestMapping(value ="uploadFileStore.do", method = RequestMethod.POST)
public void uploadFileStore(HttpServletResponse response, FileStore fileStore, @RequestParam("wj") MultipartFile wj, HttpServletRequest request) throws IOException {
if (wj == null) {
ResponseUtils.textWriterOfFail(response, "无法获取文件");
return;
}
//上传地址,我是保存在properties中的(方便改),这里取一下
String path = "";
try {
try {
Properties properties = PropertiesUtil.readProperties("sys.properties");
path = properties.getProperty("fs.upload");
} catch (IOException e) {
e.printStackTrace();
}

//保存一下文件信息
fileStore.setFileUrl(Upload.upload(path, wj));
fileStoreService.insert(fileStore);
} catch (Exception e) {
e.printStackTrace();
}
}


   //下载文件

@RequestMapping("downloadFileStore")
public void downloadFsFileStore(HttpServletResponse response, HttpServletRequest request, String fsId) throws IOException {
try {
FileStore fileStore = fileStoreService.queryById(fsId);
String filename = fileStore.getFileUrl().substring(fileStore.getFileUrl().lastIndexOf("/") + 1);
String path = fileStore.getFileUrl();
Upload.download(filename, path, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}


上面调用的Upload类

package com.flatform.fs.web.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

import com.flatform.frame.core.utils.UUIDUtils;







public class UploadIN {
public static String upload(String uploadpath,//path 上传路径 
MultipartFile multipartFile){
// 获取文件名
String filename = multipartFile.getOriginalFilename();
//重新来一个(这样就不用特地去截取文件类型了)
filename="("+UUIDUtils.create().substring(0, 8)+")"+filename;
// 完整路径(这一步可以无视,这里方便查找用)
Calendar calendar=Calendar.getInstance();
uploadpath=uploadpath+"/"+calendar.get(Calendar.YEAR)+"/"+
(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE);
File targetFile = new File(uploadpath, filename);
// 判断文件夹是否已经存在,如果不存在重新建
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 转存文件
try {
multipartFile.transferTo(targetFile);
return uploadpath+ "/" + filename;
} catch (Exception e) {
throw new RuntimeException("上传失败");
}
}
public static void download(String fileName, String filePath,
HttpServletRequest request, HttpServletResponse response) 
throws Exception {
   //设置响应头和客户端保存文件名
   response.setCharacterEncoding("utf-8");
   response.setContentType("multipart/form-data");
// URLEncoder.encode(fileName, "UTF-8")改一下标题的编码格式,不然可能乱码
   response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));

   try {
    File file = new File(filePath);
       //打开本地文件流
       InputStream inputStream = new FileInputStream(file);
       //激活下载操作
       OutputStream os = response.getOutputStream();

       //循环写入输出流
       byte[] b = new byte[2048];
       int length;
       while ((length = inputStream.read(b)) > 0) {
           os.write(b, 0, length);
       }        
       // 这里主要关闭
       os.close();
       inputStream.close();
   } catch (Exception e){
       throw e;
   }
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值