2017-04-20 75 views
0

我正在開發中,我有一個用的index.jsp形式的Web應用程序上傳文件到servlet:如何在將文件上傳到servlet後管理html網站?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    import="com.uteva.AppWebNieve.ProcesadoExcel" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets" lang="es" xml:lang="es"> 

     <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO- 
     8859-1"> 
      <title>AppWebInformesNieve</title> 
     </meta> 
    </head> 
    <body> 


     <form action="upload" method="post" enctype="multipart/form-data"> 
     <input type="file" name="file" /> 
      <input value="Subir Excel" type="submit" /> 
     </form> 

      </body> 
     </html> 

然後我上傳文件正確與我的java類:

  @Override 
     protected void doPost(HttpServletRequest 
       request,HttpServletResponse response) throws ServletException, 
      IOException{ 
     //***************AQUI MANEJAMOS LA SUBIDA DEL FICHERO LINCES DE INCIDENCIAS*********************** 
     // Get the file location where it would be stored. 
      //String filePath = getServletContext().getInitParameter("file-upload");    
      boolean isMultipart;  
      int maxFileSize = 10000 * 1024; 
      int maxMemSize = 1000 * 1024; 
      File file ; 
     // Check that we have a file upload request 
      isMultipart = ServletFileUpload.isMultipartContent(request); 
      response.setContentType("text/html"); 
      java.io.PrintWriter out = response.getWriter(); 
      if(!isMultipart){ //No se ha podido subir el fichero 
      /*out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet upload</title>"); 
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<p>No file uploaded</p>"); 
      out.println("</body>"); 
      out.println("</html>");*/ 
      return; 
      } 
      DiskFileItemFactory factory = new DiskFileItemFactory(); 
      // maximum size that will be stored in memory 
      factory.setSizeThreshold(maxMemSize); 
      // Location to save data that is larger than maxMemSize. 
      factory.setRepository(new File("c:\\temp")); 

      // Create a new file upload handler 
      ServletFileUpload upload = new ServletFileUpload(factory); 
      // maximum file size to be uploaded. 
      upload.setSizeMax(maxFileSize); 

      try{ 
      // Parse the request to get file items. 
      List fileItems = upload.parseRequest(request); 

      // Process the uploaded file items 
      Iterator i = fileItems.iterator(); 

      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Servlet upload</title>");   
      out.println("</head>"); 
      out.println("<body>"); 
      while (i.hasNext()) 
      { 
      FileItem fi = (FileItem)i.next(); 
      if (!fi.isFormField()) 
      { 
       // Get the uploaded file parameters 
       String fieldName = fi.getFieldName(); 
       String fileName = fi.getName(); 
       String contentType = fi.getContentType(); 
       boolean isInMemory = fi.isInMemory(); 
       long sizeInBytes = fi.getSize(); 
       // Write the file 
       if(fileName.lastIndexOf("\\") >= 0){ 
        file = new File(filename + 
        fileName.substring(fileName.lastIndexOf("\\"))) ; 
       }else{ 
        file = new File(filename + 
        fileName.substring(fileName.lastIndexOf("\\")+1)) ; 
       } 
       fi.write(file) ; 
       out.println("Uploaded Filename: " + fileName + "<br>"); 
       System.out.println("Fichero "+filename+" subido correctamente al servidor!!"); 
      } 
      } 
      out.println("</body>"); 
      out.println("</html>"); 
     }catch(Exception ex) { 
      System.out.println(ex); 
     } 
     } 

但上傳後,這打開一個新的簡單的HTML,這是建立在以前的java方法out.println ...

關鍵是,我怎麼可以管理這個上傳留在相同的HTML知道是否文件已經上傳或者沒有這樣做我有這個文件的其他事情?我不能使用get方法來上傳文件,並讓表單動作變爲空白...

或者,打開一個新的html,必須有另一種方法來製作更復雜的html,而不是在Java函數中使用out.println ....

謝謝。

+0

我想我需要用Javascript來上傳文件 –

回答

相關問題