2016-05-16 163 views
0

我做了應該將文件上載到一個目錄JSP,它表明我嘗試連接時出現錯誤: 在這裏,你可以看到我的代碼:JSP上傳文件錯誤

如果有人能幫助我,我會appriciate它。 package src;

import java.io.File; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.List; 
import javax.servlet.RequestDispatcher; 
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.FileItemFactory; 
import org.apache.commons.fileupload.FileUploadBase; 
import org.apache.commons.fileupload.disk.DiskFileItemFactory; 
import org.apache.commons.fileupload.servlet.ServletFileUpload; 
import org.apache.commons.io.FilenameUtils; 


public class Controller extends HttpServlet { 

/** 
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
* methods. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 


String saveFile="c:\\uploaddir\\"; 


protected void processRequest(HttpServletRequest request,   HttpServletResponse response) 
      throws ServletException, IOException 
{ 
    String actiune="afiseaza"; 
    PrintWriter out=response.getWriter(); 
    if (request.getParameter("actiune")!=null) 
    { 
     actiune = request.getParameter("actiune"); 
    } 

    if (actiune.equals("afiseaza")) 
    { 
     doAfiseaza(request, response); 
    } 

    if (actiune.equals("sterge")) 
    { 
     doDeleteFile(request, response); 
    } 

    try{ 
      boolean ismultipart=ServletFileUpload.isMultipartContent(request); 
      if(!ismultipart) 
      { 

      } 
      else 
      { 
       FileItemFactory factory= new DiskFileItemFactory(); 
       ServletFileUpload upload= new ServletFileUpload(factory); 
       List items=null; 

       try{ 
        items=upload.parseRequest(request); 

       }catch(Exception e) 
       { 

       } 

       Iterator itr=items.iterator(); 
       while(itr.hasNext()) 
       { 
        FileItem item= (FileItem)itr.next(); 

        if(item.isFormField()) 
        { 

        } 
        else 
        { 
         String itemname=item.getName(); 
         if((itemname==null)||itemname.equals("")) 
         { 
          continue; 
         } 
         String filename=FilenameUtils.getName(itemname); 
         File f=checkExist(filename); 
         item.write(f); 

        } 


       } 

      } 

     }catch(Exception e) 
     { 

     } 
     finally{ 
     out.close(); 




      } 
} 
private void doDeleteFile(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException 
{ 
    String fName=URLDecoder.decode(request.getParameter("fis")); 

    File f = new File("c:\\uploaddir\\"+fName); 
    f.delete(); 

    doAfiseaza(request, response); 
} 

private void doAfiseaza(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException 
{ 
    String dir = "c:\\uploaddir"; 
    int er = 0; 
    File[] lista = null; 

    try 
    { 
     File f = new File(dir); 
     lista = f.listFiles(); 
    } 
    catch(Exception ex) 
    { 
     er=-1; 
    } 

    request.setAttribute("eroare", new Integer(er)); 
    request.setAttribute("lista", lista); 

    RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/lista.jsp"); 
    rd.forward(request, response); 
} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
/** 
* Handles the HTTP <code>GET</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Handles the HTTP <code>POST</code> method. 
* 
* @param request servlet request 
* @param response servlet response 
* @throws ServletException if a servlet-specific error occurs 
* @throws IOException if an I/O error occurs 
*/ 
@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    processRequest(request, response); 
} 

/** 
* Returns a short description of the servlet. 
* 
* @return a String containing servlet description 
*/ 
@Override 
public String getServletInfo() { 
    return "Prima aplicatie"; 
}// </editor-fold> 

private File checkExist(String fileName) { 

    File f=new File(saveFile+"/"+fileName); 

    if(f.exists()) 
    { 
     StringBuffer sb=new StringBuffer(fileName); 
     sb.insert(sb.lastIndexOf("."), "-"+new Date().getTime()); 
     f=new File(saveFile+"/"+sb.toString()); 

    } 
    return f; 
} 

}

所以現在我沒有錯誤,但它不上傳我的文件夾,所以我不知道該怎麼辦。

+0

請將您的代碼粘貼到問題中,以便閱讀更容易。我剛纔看了一下代碼和錯誤消息:「解析時到達文件末尾」,我猜你已經在代碼中的某個地方遺漏了右括號「}」。 – Adam

+0

我把它丟在那裏,但它顯示了另一個錯誤,當我嘗試提交該文件。我編輯了這個問題,請看上面。 –

+0

如果你能幫助我,請@亞當,這將是很好的。 –

回答

0

由於您原來的問題是關於JSP,因此這裏有一些演示代碼。

<%@ page import="java.util.*, 
      java.io.*, 
      org.apache.commons.fileupload.*, 
      org.apache.commons.fileupload.servlet.*, 
      org.apache.commons.fileupload.disk.*, 
      org.apache.commons.fileupload.util.*"%> 
<% 
    boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
    out.print("Request content length is " + request.getContentLength() + "<br/>"); 
    if(isMultipart){ 
     ServletFileUpload upload = new ServletFileUpload(); 
     FileItemIterator iter = upload.getItemIterator(request); 
     FileItemStream item = null; 
     String name = ""; 
     InputStream stream = null; 
     while (iter.hasNext()){ 
      item = iter.next(); 
      name = item.getFieldName(); 
      stream = item.openStream(); 
      if(item.isFormField()){out.println("Form field " + name + " with value " 
              + Streams.asString(stream) + "<br/>");} 
       else { 
        name = item.getName(); 
        if(name != null && !"".equals(name)){ 
         String fileName = new File(item.getName()).getName(); 
         out.println("Client file " + item.getName() + " <br/>with file name " 
                + fileName + " uploaded.<br/>"); 

         File file = new File("C:/uploaddir/" + fileName); 
         FileOutputStream fos = new FileOutputStream(file); 
         long fileSize = Streams.copy(stream, fos, true); 
         out.print(fileName 
          + " was uploaded to the fileUploads folder. Size was " + 
            fileSize + "<br/>"); 
         } 
        } 
     } 
    } else out.print("Wrong request type!"); 
%> 

調用JSP與

<html><body> 
     <form enctype="multipart/form-data" action="fileUpload3.jsp" method="post"> 
       <input type='file' name='file1'/><br/> 
       <input type='file' name='file2'/><br/> 
       <input type='file' name='file3'/><br/> 
       Type message:<input type="text" name="message" /><br/> 
       <input Type='submit' value='Submit'/> 
     </form> 
</body></html> 

把兩個文件在你的web應用程序的根文件夾。