Apache commons文件上传不上传和重命名所有文件

时间:2014-02-26 01:34:50

标签: apache servlets file-upload tomcat7

我必须将6个文件上传到服务器并重命名,但由于某些原因,下面的代码似乎正在本地主机上工作并正确上传所有内容,但是相同的代码片段在托管时无法在服务器上运行。我有时会重命名和上传2个文件或3个文件。有人可以看看这段代码并给我一些意见。

 } else if (!item.isFormField()) {
                                       long size = item.getSize();
                    String contentType = item.getContentType();
                   System.out.println("Field Name = " + item.getFieldName()
                            + ", File Name = " + item.getName()
                            + ", Content type = " + item.getContentType()
                            + ", File Size = " + item.getSize()
                            + ", boolean isInMemory = " + item.isInMemory());
                    String fieldname = item.getFieldName();
                    String name = item.getName();
                    if ((fieldname.equals("img1")) && name != null) {
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName1 is " + ImageName + "'");                           
                            businessfForm.setImg1(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }

                    } else if ((fieldname.equals("img2"))&& name != null){
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName2 is " + ImageName + "'");                           
                            businessfForm.setImg2(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }
                    } else if ((fieldname.equals("img3"))&& name != null) {
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName3 is " + ImageName + "'");                           
                            businessfForm.setImg3(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }
                    } else if ((fieldname.equals("img4"))&& name != null){
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName4 is " + ImageName + "'");                           
                            businessfForm.setImg4(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }
                    }  else if ((fieldname.equals("img5"))&& name != null){
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName5 is " + ImageName + "'");                           
                            businessfForm.setImg5(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }
                    } else if ((fieldname.equals("img6"))&& name != null){
                        if ((size < 1048576) && (("image/jpeg".equals(contentType)) || ("image/jpg".equals(contentType)) || ("image/gif".equals(contentType)) || ("image/png".equals(contentType)) || ("image/bmp".equals(contentType)))) {
                            String filetype = name.substring(name.lastIndexOf("."));
                            ImageName = System.currentTimeMillis() + filetype;
                            System.out.println(" ImageName6 is " + ImageName + "'");                           
                            businessfForm.setImg6(ImageName);
                        } else {
                            request.setAttribute("ImageError", ImageError);
                        }
                    }         
                    File file = new File(destinationDir, ImageName);
                   try {
                        item.write(file);                        
                    } catch (Exception ex) {
                        Logger.getLogger(xxx.class.getName()).log(Level.SEVERE, "Failed to parse upload request", ex);
                    }
                }

更新。打印语句输出如下,

Field Name = img1, File Name = 1.jpg, Content type = image/jpeg, File Size = 24236, boolean isInMemory = false
 ImageName1 is 1393377151014.jpg'
Field Name = img2, File Name = 2 (2).jpg, Content type = image/jpeg, File Size = 240476, boolean isInMemory = false
 ImageName2 is 1393377151015.jpg'
Field Name = img3, File Name = 2.jpg, Content type = image/jpeg, File Size = 27127, boolean isInMemory = false
 ImageName3 is 1393377151015.jpg'
Field Name = img4, File Name = 3 (2).jpg, Content type = image/jpeg, File Size = 295437, boolean isInMemory = false
 ImageName4 is 1393377151015.jpg'
Field Name = img5, File Name = 4 (2).jpg, Content type = image/jpeg, File Size = 248775, boolean isInMemory = false
 ImageName5 is 1393377151015.jpg'
Field Name = img6, File Name = 5.jpg, Content type = image/jpeg, File Size = 30309, boolean isInMemory = false
 ImageName6 is 1393377151015.jpg'

0 个答案:

没有答案