//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类
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;
}
}
}