This example for you to share the Java file Upload technology for your reference, the specific contents are as follows
Form:
The client sends HTTP must use the Multipart/form-data data type, representing the composite data type. That
Use HTML tags in your form.
Required Packages:
Commons-fileupload.jar, the core upload file tool is in this package.
commons-io.jar– the package required to upload the file
Upload File class Detailed:
diskfileitemfactory-creates the file directory of the warden, which is the size of the buffer
Servletfileupload is used to parse httpservletrequest. Returns a set of File objects.
fileitem– represents the image of each file uploaded by the user.
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Transition Board – Understanding the bottom
Package cn.hncu.servlet;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Upload0servlet extends HttpServlet {public
void DoPost (HttpServletRequest request, HttpServletResponse response)
throws Servletexception, IOException {inputstream in=
Request.getinputstream ();
BufferedReader br=new BufferedReader (New InputStreamReader (in));
String Line;
while ((Line=br.readline ())!=null) {
System.out.println (line);}}}
Uploading files using the Apache File Upload Tool
Package cn.hncu.servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
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.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.io.FileUtils; public class Uploadservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse resp
Onse) throws Servletexception, IOException {response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter (); Out.println ("<!
DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>"); Out.println ("&Lt
Head><title>a servlet</title>
Store uploaded files in this place.
Upload information:
Upload results:
Use Apache File Upload tool to achieve file Upload 2 (solve file name garbled)
Package cn.hncu.servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
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.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.io.FileUtils; public class Upload2servlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Res
Ponse) throws Servletexception, IOException {response.setcontenttype ("text/html"); If it is a form with uploaded files (POST2), the play can only set the encoding of the file name of the uploaded files (solve his Chinese garbled code)//But can not solve the common form components in the POST2 mode of the Chinese garbled printwriter out = Response.getwrit
ER (); Out.println ("<! DOCTYPE HTML public \ "-//w3c//dtD HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println ("
Upload information:
Upload results:
Use Apache File Upload tool to achieve File upload 3 (file scattered)
Optimizing file storage with a hash directory:
A hash directory is a way to optimize file storage performance. Whether it is Windows or Linux, NTFS or EXT3, the number of items that can be accommodated in each directory is limited.
It's not that you can't save, it's when the number of items is too large, it slows down the file index,
So it is necessary to weigh how many files should be saved in a directory. A lot of storage will affect performance, save less will cause too much directory and space waste. So when you save a large number of files,
It is necessary to have an algorithm that can "scatter" files in different subdirectories to improve indexing speed at each level, this algorithm is Hash. Usually used in MD5, SHA1, etc. can be used to do hash directory, my session also used the MD5, get SessionID First and nineth, which constitutes a two-level hash path, that is, the system of all the session file dispersed to 16x 16=256 the subdirectory. Assuming that Linux saves 1000 files per directory to achieve the best spatial performance ratio, the system can ideally have 256,000 session files in use at the same time.
Package cn.hncu.servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
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.FileUploadException;
Import Org.apache.commons.fileupload.ProgressListener;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.io.FileUtils; public class Upload3servlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Res
Ponse) throws Servletexception, IOException {response.setcontenttype ("text/html"); If it is a form with uploaded files (POST2), the play can only set the encoding of the file name of the uploaded files (solve his Chinese garbled code)//But can not solve the common form components in the POST2 mode of the Chinese garbled printwriter out = Response.getwrit ER ();
Out.println ("<!
DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println ("
Breaking information:
Scatter the result:
Document 1:
Document 2:
Demo Upload Progress principle
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.