File upload is a very common function of the site, direct use of the servlet to get upload files have to parse request parameters, more trouble, so generally choose to use the Apache open source tools, Common-fileupload. This jar pack can be found on the Apache website, or it can be found under the Lib folder of struts, and the function of struts is based on this implementation.
Common-fileupload is dependent on the Common-io package, so it is also necessary to download the package. Then import to your project path below.
Use the following code
Package oop.hg.ytu.servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Oop.hu.ytu.dao.UploadDomain;
Import Org.apache.commons.fileupload.FileItem;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload; public class Upload extends HttpServlet {/** * processing user upload request/private static final long Serialversionuid = 1
L
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
String describe = Request.getparameter ("describe");
Diskfileitemfactory factory = new Diskfileitemfactory (); @SuppressWarnings ("deprecation") String Path = Request.getrealpath ("/upload")//Set disk slowChong Path Factory.setrepository (new File (path));
Factory.setsizethreshold (1024*1024);/set Create buffer size servletfileupload upload = new Servletfileupload (factory); Upload.setsizemax (-1);//Set upload file limit size,-1 No Limit try {@SuppressWarnings ("unchecked") list<fileitem> lis
t = upload.parserequest (request);
String va = null;
for (Fileitem item:list) {//String name = Item.getfieldname ();
if (Item.isformfield ()) {//To determine whether the file stream va = item.getstring ("UTF-8");
System.out.println (name+ "=" +va);
Request.setattribute (name, value);
}else{String value = Item.getname ()//will pass the full path name int start = Value.lastindexof ("\");
String fileName = value.substring (start+1);
Request.setattribute (name, fileName);
InputStream in = Item.getinputstream ();
Uploaddomain dao = new Uploaddomain (); Item.write (New File (Realpath,filename));
int index = Filename.lastindexof (".");
String realfilename = filename.substring (0,index);
String type = filename.substring (index+1);
Dao.insert (in, Realfilename,type,va);//Put into database}} catch (Exception e) {
E.printstacktrace (); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I
oexception {doget (request, response); }
}
Here to determine whether the upload stream or the parameters inside the form, such as text box submission information, and then insert them into the database. The database inserts
code as follows
Package Oop.hu.ytu.dao;
Import Java.io.InputStream;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Oop.hg.ytu.utils.JdbcUtils; /** * provides file upload support * @author Administrator */public class Uploaddomain {/** * send uploaded files to the database/pub LIC void Insert (InputStream in, String fileName, String type,string describe) throws exception{//writes a picture to the database Connectio
n conn = null;
PreparedStatement PS = null;
ResultSet rs = null;
System.out.println (describe);
try {//2. Establish Connection conn = Jdbcutils.getconnection ();
3. Create statement String sql = INSERT INTO fileupload (file,filename,type,des) values (?,?,?,?);
PS = conn.preparestatement (SQL);
Ps.setblob (1, in);
Ps.setstring (2, fileName);
Ps.setstring (3, type);
Ps.setstring (4, describe);
4. Execution statement ps.executeupdate ();
In.close (); finally {JdbcutiLs.free (RS, PS, conn); }
}
}
You may experience a database default asking price size limit, and you need to change the following configuration below the My.ini under the MySQL installation directory.
[Mysqld]
max_allowed_packet=64m
That's it. Of course, note the encoding format. Upload files to fix. And there's just one of my column names set to describe, the result and MySQL reserved word flushes
Sudden, appear cannot insert information phenomenon, must pay attention to later.
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.