在spring 框架中本身存在着这么一个类,你配置完SSM框架,导入了org.springframework.web 这个jar包,就一定存在以下这个类
org.springframework.web.multipart
接下来是完成步骤,
一、将 MultipartFile 对象作为形参加入到add方法中
二、为了实现上传和下载功能,我们需要配置两个路径,真实路径和虚拟路径(下载路径)
String apkLocPath = null;
String apkFileName = null;
三、配置两个路径的参数
String path = request.getSession().getServletContext()
.getRealPath("statics" + File.separator + "uploadfiles");
downloadLink = request.getContextPath() + "/statics/uploadfiles/" + apkFileName;
apkLocPath = path + File.separator + apkFileName;
四,当然,为了防止文件重名导致覆盖掉同名的文件,我们还需要重新给每个文件都给一个唯一名字
String oldFileName = attach.getOriginalFilename();// 原文件名
String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
if (prefix.equalsIgnoreCase("apk")) {// apk文件命名:apk名称+版本号+.apk
String apkName = null;
try {
apkName = appInfoService.getAppInfo(appVersion.getAppId(), null).getAPKName();
} catch (Exception e1) {
e1.printStackTrace();
}
if (apkName == null || "".equals(apkName)) {
return "redirect:/dev/flatform/app/appversionadd?id=" + appVersion.getAppId() + "&error=error1";
}
apkFileName = apkName + "-" + appVersion.getVersionNo() + ".apk";
File targetFile = new File(path, apkFileName);
五、为了解决下载路径的问题,我们需要配置TOMCAT的虚拟路径,解决方案如下
1、打开已经配置好的TOMCAT
2、找到Deployment,点击添加符号加入所需要下载文件所在的真实的路径(需指定到文件的父级包)
3、修改 Application context 的参数(即添加虚拟路径,注意添加多个不能同名)
六,运行项目,这个时候就已经能够正常的通过虚拟路径实现下载功能
具体完整方法如下,大家可以自己参考下
/**
* 保存新增appInfo(主表)的数据
*
* @param appInfo
* @param session
* @return
*/
@RequestMapping(value = "/appinfoaddsave", method = RequestMethod.POST)
public String addSave(AppInfo appInfo, HttpSession session, HttpServletRequest request,
@RequestParam(value = "a_logoPicPath", required = false) MultipartFile attach) {
String logoPicPath = null;
String logoLocPath = null;
if (!attach.isEmpty()) {
String path = request.getSession().getServletContext()
.getRealPath("statics" + java.io.File.separator + "uploadfile");
String oldFileName = attach.getOriginalFilename();// 原文件名
String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
int filesize = 500000;
if (attach.getSize() > filesize) {// 上传大小不得超过 50k
request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_4);
return "developer/appinfoadd";
} else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
|| prefix.equalsIgnoreCase("jepg") || prefix.equalsIgnoreCase("pneg")) {// 上传图片格式
String fileName = appInfo.getAPKName() + ".jpg";// 上传LOGO图片命名:apk名称.apk
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
attach.transferTo(targetFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_2);
return "developer/appinfoadd";
}
logoPicPath = request.getContextPath() + "/statics/uploadfile/" + fileName;
logoLocPath = path + File.separator + fileName;
} else {
request.setAttribute("fileUploadError", Constants.FILEUPLOAD_ERROR_3);
return "developer/appinfoadd";
}
}
appInfo.setCreatedBy(((DevUser) session.getAttribute(Constants.DEV_USER_SESSION)).getId());
appInfo.setCreationDate(new Date());
appInfo.setLogoPicPath(logoPicPath);
appInfo.setLogoLocPath(logoLocPath);
appInfo.setDevId(((DevUser) session.getAttribute(Constants.DEV_USER_SESSION)).getId());
appInfo.setStatus(1);
try {
if (appInfoService.add(appInfo)) {
return "redirect:/dev/flatform/app/list";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "developer/appinfoadd";
}