代码块:

public void add(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException, IllegalAccessException, InvocationTargetException{
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload sf = new ServletFileUpload(fileItemFactory);
if (sf.isMultipartContent(request)) {
Map<String, String> paramMap = new HashMap<String, String>();
List<FileItem> filelist = sf.parseRequest(request);
for(FileItem fileItem : filelist){
if(!fileItem.isFormField()){
String path = getServletContext().getRealPath("/products");
InputStream is = fileItem.getInputStream();
FileOutputStream fos = new FileOutputStream(new File(path, fileItem.getName()));
IOUtils.copy(is, fos);
fileItem.delete();
is.close();
fos.close();
paramMap.put(fileItem.getFieldName(), "products/" + fileItem.getName());
}else {
paramMap.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
}
}
Product product = new Product();
BeanUtils.populate(product, paramMap);
boolean result = ps.addProduct(product);
if (result) {
response.getWriter().println("<script>confirm('添加成功');location.href='/Shopping/product?method=list'</script>");
}else{
response.getWriter().println("<script>confirm('添加失败');location.href='/Shopping/admin/product/add.jsp'</script>");
}
}
}

错误信息:

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.js.web.servlet.BaseServlet.service(BaseServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.io.FileNotFoundException: D:\Myeclipse\workspace\.metadata\.me_tcat\webapps\Shopping\products\C:\Users\xl\Desktop\吃鸡\one.png (文件名、目录名或卷标语法不正确。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at com.js.web.servlet.ProductServlet.add(ProductServlet.java:42)

... 18 moreform表单文件上传图片报错处理

处理方法:

代码块中红色的两行代码改为:

①FileOutputStream fos = new FileOutputStream(new File(path, new File(fileItem.getFieldName()).getName()));

②paramMap.put(fileItem.getFieldName(), "products/" + new File(fileItem.getFieldName()).getName());

因为两行获取文件地址错误,所以上传不了图片,找不到此图片

相关文章: