java는 여러 개의 파일을 동적으로 업로드하고 파일 이름 바꾸기 문제를 해결합니다

본고는 두 가지 측면으로 나누어 설명한다.
1. 자바는 여러 개의 파일을 동적으로 업로드합니다.
2. 파일 이름 바꾸기 문제 해결
여러분의 참고를 제공하며, 구체적인 내용은 아래와 같습니다.
1. 여러 파일을 동적으로 업로드

 <form name="xx" action="<c:url value='/Up3Servlet'/>" method="post" enctype="multipart/form-data">
  <table id="tb" border="1">
    <tr>
      <td>
        File:
      </td>
      <td>
        <input type="file" name="file">
        <button onclick="_del(this);"> </button>
      </td>
    </tr>
  </table>
  <br/>
  <input type="button" onclick="_submit();" value=" ">
  <input onclick="_add();" type="button" value=" ">
  </form>
 </body>
 <script type="text/javascript">
   function _add(){
     var tb = document.getElementById("tb");
     // 
     var tr = tb.insertRow();
     // 
     var td = tr.insertCell();
      // 
     td.innerHTML="File:";
     // td
     var td2 = tr.insertCell();
     // input
     td2.innerHTML='<input type="file" name="file"/><button onclick="_del(this);"> </button>';
   }
   function _del(btn){
     var tr = btn.parentNode.parentNode;
     //alert(tr.tagName);
     // tr table 
     var index = tr.rowIndex;
     // 
     var tb = document.getElementById("tb");
     tb.deleteRow(index);
   }
   function _submit(){
     // 
     var files = document.getElementsByName("file");
     if(files.length==0){
       alert(" ");
       return false;
     }
     for(var i=0;i<files.length;i++){
       if(files[i].value==""){
         alert(" "+(i+1)+" ");
         return false;
       }
     }
    document.forms['xx'].submit();
   }
 </script>
</html>
업로드할 모든 파일을 훑어보다
2. 파일의 이름 변경 문제 해결

package cn.hx.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;

public class UpImgServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    String path = getServletContext().getRealPath("/up");
    DiskFileItemFactory disk = 
        new DiskFileItemFactory(1024*10,new File("d:/a"));
    ServletFileUpload up = new ServletFileUpload(disk);
    try{
      List<FileItem> list = up.parseRequest(request);
      // *.jpg-iamge/jpege.,bmp/imge/bmp,png,
      List<String> imgs = new ArrayList<String>();
      for(FileItem file :list){
        if(file.getContentType().contains("image/")){
          String fileName = file.getName();
          fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
          
          // 
          String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
          //UUID
          String uuid = UUID.randomUUID().toString().replace("-", "");
          // 
          String newName = uuid+extName;     // UUID , 
          
          
          FileUtils.copyInputStreamToFile(file.getInputStream(),
              new File(path+"/"+newName));
          // list
          imgs.add(newName);
        }
        file.delete();
      }
      request.setAttribute("imgs",imgs);
      request.getRequestDispatcher("/jsps/imgs.jsp").forward(request, response);
    }catch(Exception e){
      e.printStackTrace();
    }
  
  }

}
이상의 자바 다중 파일 업로드를 실현하고 파일 이름 변경 문제를 해결하여 여러분의 학습에 도움이 되기를 바랍니다.

좋은 웹페이지 즐겨찾기