Commons-fileupload implements upload -- JAVA
In many Web site applications, users must be provided with the function of uploading documents and materials through browsers, such as uploading personal photos and sharing documents. This function is available in DRP. You need to upload and display the corresponding material images.
The upload function provides excellent support on the browser side. You only need to obtain and save the File Uploaded By the browser on the Web server side. To simplify and help Web developers receive files uploaded by browsers, some companies and organizations have developed File Upload components, such as commons-fileupload. Now let's take a look at how this function is implemented.
Requirement: Upload and display the corresponding material Image
Introduce the jar package:
First introduce two jar packages, one is the commons-fileupload.jar package, the other is the commons-io-2.2.jar package.
How does the browser support:
Use post to submit;
Modify the enctype attribute of form to enctype = "multipart/form-data", that is:
I think the two methods are quite good about where to save the uploaded image:
First, save the image to a local disk. The image name corresponds to the item number.
Type 2: Save the image to a local disk and save the image name in the database.
The two methods are similar. The second method is described in detail.
Instance:
Save the image to a local disk and the image name in the database.
In item_upload.jsp
<% Item item = (Item) request. getAttribute ("item"); // get the value %>
Upload the file in Servlet, write the image to the corresponding directory, and save the image name in the database:
public class FileUploadServlet extends HttpServlet {File uploadPath=null;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
// Obtain the item information ItemManager itemManager = new ItemManagerImpl (); String itemNo = request. getParameter ("itemNo"); Item item = itemManager. findItemById (itemNo); // query all information of the item request. setAttribute ("item", item); request. getRequestDispatcher ("/basedata/item_upload.jsp "). forward (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// String uploadPath = "C: \ Users \ ZhuDan \ Desktop \ Drp \ DRP system \ apache-tomcat-7.0.6 \ webapps \ drp3.9 \ Upload "; boolean isMultipart = ServletFileUpload. isMultipartContent (request); ItemManager itemManager = new ItemManagerImpl (); StringitemNo = ""; String fileName = ""; if (isMultipart = true) {try {FileItemFactory factory = new DiskFileItemFactory (); ServletFileUpload upload = new ServletFileUpload (factory); List
Items = upload. parseRequest (request); // get all the files Iterator
Itr = items. iterator (); while (itr. hasNext () {// process each file in sequence FileItem item = (FileItem) itr. next (); // obtain all information of each file // It is a normal form type if (item. isFormField () {if ("itemNo ". equals (item. getFieldName () {itemNo = item. getString () ;}} fileName = item. getName (); // get the file name, including path D: \ Life \ photo \ Friends \ hoof \ Gao Rao shan.jpg if (fileName! = Null) {File fullFile = new File (item. getName (); // path of the folder to be saved: File savedFile = new File (uploadPath, fullFile. getName (); // capture a string, for example, C: \ WINDOWS \ Debug \ PASSWD. LOG fileName = fileName. substring (fileName. lastIndexOf ("\") + 1, fileName. length (); if (itemNo! = Null & fileName! = Null) {itemManager. uploadItemImage (itemNo, fileName);} // save it to the folder item. write (savedFile); // File fullFile = new File (itemNo); // File savedFile = new File (uploadPath, fullFile. getName () + ". jpg ") ;}// System. out. print ("upload succeed");} catch (Exception e) {e. printStackTrace ();} finally {response. sendRedirect ("QueryServlet") ;}} public void init () throws ServletException {uploadPath = new File (getServl EtContext (). getRealPath ("Upload"); // obtain the path of the Upload folder used to save the image if (! UploadPath. exists () {// If the Upload Folder does not exist, create uploadPath. mkdir ();}}}
The interface effect is as follows:
Fields in the database: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICAgICAgICA8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140729/20140729092610343.png" alt = "\">
In the corresponding local folder (tomcat:
The component regards all elements submitted by the page (common form fields, such as input and file fields) as the same FileItem, in this way, the request submitted on the upload page is an ordered combination of FileItem. The FileUpload component can parse the request and return a FileItem. For each FileItem, The FileUpload component can determine whether it is a common form field or a file field, so that different operations can be taken based on different types-if it is a form field, read the value. If it is a file field, save the file to the server hard disk or memory.