easyui结合spring mvc 如何上传图片
1个回答
推荐于2016-09-10
展开全部
SpringMVC实现文件上传,直接上代码:
后台代码:
@RequestMapping(value = "/uploadApk")
@ResponseBody
public Object uploadApk(
@RequestParam(value = "apkFile") MultipartFile apkFile,
HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> resMap = new HashMap<String,Object>();
if (apkFile != null) {
//获取保存的路径,
String realPath = request.getSession().getServletContext()
.getRealPath("/upload/apk");
if (apkFile.isEmpty()) {
// 未选择文件
resMap.put("status", StatusConstants.STATUS_PARM_IS_EMPTY);
} else{
// 文件原名称
String originFileName = apkFile.getOriginalFilename();
try {
//这里使用Apache的FileUtils方法来进行保存
FileUtils.copyInputStreamToFile(apkFile.getInputStream(),
new File(realPath, originFileName));
resMap.put("status",StatusConstants.STATUS_OK);
} catch (IOException e) {
System.out.println("文件上传失败");
resMap.put("status", StatusConstants.STATUS_EXECPTION);
e.printStackTrace();
}
}
}
return resMap;
}
后台代码:
@RequestMapping(value = "/uploadApk")
@ResponseBody
public Object uploadApk(
@RequestParam(value = "apkFile") MultipartFile apkFile,
HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> resMap = new HashMap<String,Object>();
if (apkFile != null) {
//获取保存的路径,
String realPath = request.getSession().getServletContext()
.getRealPath("/upload/apk");
if (apkFile.isEmpty()) {
// 未选择文件
resMap.put("status", StatusConstants.STATUS_PARM_IS_EMPTY);
} else{
// 文件原名称
String originFileName = apkFile.getOriginalFilename();
try {
//这里使用Apache的FileUtils方法来进行保存
FileUtils.copyInputStreamToFile(apkFile.getInputStream(),
new File(realPath, originFileName));
resMap.put("status",StatusConstants.STATUS_OK);
} catch (IOException e) {
System.out.println("文件上传失败");
resMap.put("status", StatusConstants.STATUS_EXECPTION);
e.printStackTrace();
}
}
}
return resMap;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询