当前位置:我的异常网» Java Web开发 » 前台:Input type="file" 后台获取文件内
前台:Input type="file" 后台获取文件内容用的是spring注解,当地环境上传图片是好的,发布到服务器上图片读取不到,求大神指点
www.myexceptions.net 网友分享于:2015-08-26 浏览:0次
前台:Input type="file" 后台获取文件内容用的是spring注解,本地环境上传图片是好的,发布到服务器上图片读取不到,求大神指点!
前台
*
后台
@RequestMapping("/userMember_editShowShop.do")
public void userMember_editShowShop(@ModelAttribute ShowShop showShop,
HttpSession session,HttpServletRequest request,HttpServletResponse response,
ModelMap map){
try {
if(null!=showShop){
if(null!=showShop.getId()){
Propertyuser user = (Propertyuser) session.getAttribute("user");
showShop.setCreateBy(user.getId());
//上传的图片保存在showShop里面,用的是File对象
File picture = showShop.getPicture();
System.out.println("-----------------〉:"+picture.getAbsolutePath());
if(picture != null){
String path = Config.getInstance().getProperty("phoneRootPath");
if(path == null){
path = "E:/jlx51/upload/";
}else{
if(!path.endsWith("/")){
path = path + "/";
}
path = path + System.currentTimeMillis() + ".jpg";
}
try {
UploadUtils.saveTo(picture, path);
} catch (IOException e) {
e.printStackTrace();
}
showShop.setPhoto(path);
}
showShopService.updateShowShop(showShop);
request.getRequestDispatcher("userMember_getShowShopList.do").forward(request,response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveTo(File file , String path) throws IOException{
byte[] data = FileUtils.readFileToByteArray(file);
File f = new File(path);
if(f.exists()){
f.delete();
}
if(!f.getParentFile().exists()){
f.getParentFile().mkdirs();
}
FileUtils.writeByteArrayToFile(f, data);
}
服务器报的错误是:
java.io.FileNotFoundException: File '123.jpg' does not exist
at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:136)
at org.apache.commons.io.FileUtils.readFileToByteArray(FileUtils.java:994)
at com.jlx51.utils.UploadUtils.saveTo(UploadUtils.java:15)
at com.jlx51.config.spring.mvc.ShowShopController.userMember_insertShowShop(ShowShopController.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
拿到的picturePath是服务器tomcat下的路径,根本不存在:E:\www\tomcat6.0.29\Tomcat 6.0\123.jpg
求大神指点!
------解决思路----------------------
你获取picture的时候,是File picture = showShop.getPicture();请问这里你确定文件是封装到showShop对象的picture属性中了吗?spring注解文件上传参考这里
文章评论