赞
踩
MultipartFile中的void transferTo(File dest) throws IOException, IllegalStateException;
这次我推荐一个api哈!
前端js什么的就不提供了,实在要看的话,走你,看我以前的上传文件
@RequestMapping ("/uploadFile")
@ResponseBody
public String doUploadMapFile (@RequestParam (value = "uploadfile", required = false) MultipartFile uploadfile,
HttpServletRequest request)
{
Map <String, Object> data = new HashMap <String, Object> ();
boolean result = Boolean.TRUE;
// 定义容器接收文件名
String uploadFileName = null;
// 服务器存放改文件的文件名
String newUploadFileName = null;
// 文件保存路径
String filePath = null;
String visitUrl = null;
String savePath = null;
//String savePath ="D:\\CXAPP\\CXHOME\\sys\\tx\\resource\\";
//String visitUrl ="http://localhost:8080/resource/";
try
{
// 头像图片保存路径
savePath = PropertiesUtils.getPropertiesFromPlatform ("tx", "img_save_path");
// 头像图片访问路径
visitUrl = PropertiesUtils.getPropertiesFromPlatform ("tx", "img_visit_path");
// 获取上传文件名(带格式)
uploadFileName = uploadfile.getOriginalFilename ();
// 获取最后"."的位置
int lastIndex = uploadFileName.lastIndexOf (".");
// 获取上传文件的格式
String fileType = uploadFileName.substring (lastIndex, uploadFileName.length ());
// 以当前系统毫秒值为新文件名
newUploadFileName = String.valueOf (System.currentTimeMillis ()) + fileType;
File mkdir = new File (savePath + Constant.IMG_FOLDER);
if (!mkdir.exists ())
{
mkdir.mkdirs ();
}
filePath = savePath + Constant.IMG_FOLDER + File.separator + newUploadFileName;
File date = new File (filePath);
uploadfile.transferTo (date);
}
catch (Exception e)
{
result = Boolean.FALSE;
data.put ("errorMsg", "文件上传失败!");
}
data.put ("success", result);
data.put ("resultMsg", uploadFileName + "上传成功!");
data.put ("imgAddress", Constant.IMG_FOLDER + "/" + newUploadFileName);
data.put ("visitImgAddress", visitUrl + Constant.IMG_FOLDER + "/" + newUploadFileName);
String jsonStr = JSONObject.fromObject (data).toString ();
return jsonStr;
}
只有一个重点:uploadfile.transferTo (date);
这里它会把你接受的上传文件放到你前面定义的文件里去的.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。