0

在这里我想避免基于使用选择控件选择的作者姓名的重复文件名..但是在这里我没有在文件保存部分获得作者姓名为空..

FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

          try {

            List items = upload.parseRequest(request);
            Iterator iterator = items.iterator();
            while (iterator.hasNext()) {
             String author=null;
                FileItem item = (FileItem) iterator.next();
                  //form fileds
                  if(item.isFormField()){
                      String fieldName = item.getFieldName();
                      if(fieldName!=null&&fieldName.equals("author"){
                       author=item.getString();

                 }
             //files
                if (!item.isFormField()) {

                    String fileName = item.getName();
                    File path = new File("D:/uploads");
                    if (!path.exists()) {
                          boolean status = path.mkdirs();
      } 
                    File uploadedFile = new File(path + "/"+author+"_"+ fileName);
                    System.out.println(uploadedFile.getAbsolutePath());
                    item.write(uploadedFile);
                }                          

            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
4

0 回答 0