Home > front end >  File upload failed
File upload failed

Time:12-02

<body>
Name: & lt; The input name="sname"/& gt;
Upload photos: & lt; Input type="file" name="spicture"/& gt;



This is the file upload form
And then process the data of the Servlet
Package org. Student. Servlet.

Import the Java. IO. The File;
Import the Java. IO. IOException;
Import the Java. Util. Iterator;
Import the Java. Util. List;

The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. The annotation. WebServlet;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
The import javax.mail. Servlet. HTTP. HttpServletResponse;

The import org.apache.com mons. Fileupload. FileItem;
The import org.apache.com mons. Fileupload. FileItemFactory;
The import org.apache.com mons. Fileupload. FileUploadException;
The import org.apache.com mons. Fileupload. Disk. DiskFileItemFactory;
The import org.apache.com mons. Fileupload. Servlet. ServletFileUpload;


@ WebServlet ("/UploadServlet ")
Public class UploadServlet2 extends the HttpServlet {
private static final long serialVersionUID=1L;


Public UploadServlet2 () {
Super ();
}

Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
Request. SetCharacterEncoding (" utf-8 ");
The response. SetCharacterEncoding (" utf-8 ");
The response. SetContentType (" text/HTML; charset=UTF-8");
//upload
Try {
Boolean isMultipart=ServletFileUpload. IsMultipartContent (request);
If (isMultipart) {//whether the form is MUTIPART
FileItemFactory factory=new DiskFileItemFactory ();
ServletFileUpload upload=new ServletFileUpload (factory);
List The items=upload. ParseRequest (request);//all the requests of the form, save in the items in the collection
Iterator Iter=items. The iterator ();
While (iter) hasNext ()) {
FileItem item=iter. Next ();
String itemname=item. GetFieldName ();

Int sno=1;
String sname=null;

{if (item. IsFormField ())
If (itemname. Equals (" sno ")) {
Sno=Integer. ParseInt (item. Get string (" utf-8 "));
} else if (itemname equals (" sname ")) {
Sname=item. Get string (" utf-8 ");
} else {
System. The out. Println (" other fields ");
}
} else {
//file upload
//file name
The String filename=item. GetName ();
//get content and upload file
//the specified file upload location, access to the server path
String path=request. GetSession (). GetServletContext () getRealPath (" upload ");
The File File=new File (the path and filename);
Item. Write (file);
System. The out. Println (filename + "upload success");
return;
}

}
}
} the catch (FileUploadException e) {
e.printStackTrace();
}//in analytic form
The catch (Exception e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}



}


Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response);
}

}


This is not an error, but I expect to receive upload file in folder is empty,,,,,,,,, is empty, it is too difficult, also won't debugging breakpoints, hope to have a big help me run kang kang!!!!! In this thank humbly ~
  • Related