File upload has always been a very important function in the b/s structure, in Java does not have a good implementation of the file upload class package, so there are some open source components, smartupload, commons-fileupload, and a domestic "cattle man" ( Sorry to not call the name to come out, These components should be the earliest smartupload out, but now no longer have a new version, there is a shortage of smartupload support upload files up to about 100M, which can not meet the needs of customers, Heard Commons-fileupload good, after a few tests finally fix, the following is the code, 3 JSP files upload.jsp success.jsp error.jsp an action class
Upload.jsp as Follows:
<%@ page language= "java" contenttype= "text/html;charset=gbk"%>
<%@ page import= "java.util.*"%>
<%@ page import= "org.apache.commons.fileupload.*"%>
<%@ include file= "/basic/include/headerjsf1.jsp"%>
<%pagecontext.setattribute ("jsp.tags.reuse", New Boolean (false));%>
<A:navigation/>
<center><b><font size=+1><bean:message key= "upload.name"/></font></b></ Center>
<title>???? </title>
<body>
<form action= "/budget3/fileuploadaction.do" name= "one" enctype= "multipart/form-data" method= "post" >
<p align= "center" > File Upload
<input type= "File" name= "fileupload" value= "upload"/>
<input type= "submit" value= "upload" >
<input type= "reset" value= "cancel" >
</p>
</form>
</body>
Very simple, after you choose to upload the file, this page will turn to fileuploadaction.do, this class is responsible for the completion of the file upload function, the code is as Follows:
Package com.atools.budget.TestTree.action;
Import com.atools.ggcw.action.*;
Import javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;
Import org.apache.struts.action.ActionForm;
Import org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import org.apache.commons.fileupload.*;
Import org.apache.commons.io.*;
Import java.io.*;
Import java.util.*;
/**
* @author HH
*
*/
public class Fileuploadaction extends Baseaction {
Public Actionforward Execute (actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse Response)
{
Actionforward forward=null;
Try
{
System.out.println ("start");
Diskfileupload upload=new diskfileupload ();
List uploadlist=upload.parserequest (request);
System.out.println ("list is" +uploadlist);
Iterator Iter=uploadlist.iterator ();
While (iter.hasnext ())
{
Fileitem item= (fileitem) Iter.next ();
If (!item.isformfield ())
{
String Filename=item.getname ();
Filename=filenameutils.getname (filename);
If (!filename.equals (""))
{
System.out.println ("start" +filename);
String savepath= "d://temp//" +filename;
System.out.println ("savepath is" +savepath);
File Savefilepath=new file (savepath);
Item.write (savefilepath);
Forward=mapping.findforward ("success");
}else
Forward=mapping.findforward ("error");
}
}
}catch (Exception Ex)
{
Ex.printstacktrace ();
SYSTEM.OUT.PRINTLN ("the Program has an error, throws an exception for" +ex.getmessage ());
}
Return forward;
}
}
Two steering pages success.jsp and error.jsp are very simple, so it is not written here. the author tested the upload 200M file is soon completed, and smartupload the fundamental implementation of 200M or so file upload
When I looked up the information, I found a lot of it was written like This.
<%@ page language= "java" contenttype= "text/html;charset=gbk"%>
<%@ page import= "java.util.*"%>
<%@ page import= "org.apache.commons.fileupload.*"%>
<title> File Upload </title>
<%
Diskfileupload fu = new Diskfileupload ();
Set allow user to upload file size in bytes
Fu.setsizemax (10000000);
Set up to allow only data stored in memory, unit: bytes
Fu.setsizethreshold (4096);
Set the directory where the data is stored in the hard disk once the file size exceeds the value of Getsizethreshold ()
Fu.setrepositorypath ("d://tomcat5//temp");
Start reading upload information
List Fileitems = Fu.parserequest (request);
Process each uploaded file in turn
Iterator iter = Fileitems.iterator ();
While (iter.hasnext ()) {
Fileitem item = (fileitem) Iter.next ();
Ignore any other form information that is not a file field
If (!item.isformfield ()) {
String name = Item.getname ();
Item.write ("d://uploadtest//" + name);
}
}
%>
Which directly gets the file name Item.getname ();
It is then written to Item.write ("d://uploadtest//" + name), which is wrong, because Item.getname () gets the file name that includes the upload path, such as e:/work.doc, at the time of writing d:// Uploadtest/+e:/work.doc must be wrong, and a little bit. the write () method of the item requires an instance of the file class to pass in the Parameter.
The above two jar packages can be used to 1:commons-fileupload http://jakarta.apache.org/site/downloads/downloads_commons-fileupload.cgi
2: Commons-io http://jakarta.apache.org/commons/io/to Download.
There are a lot of things are still pondering, have friends who know can also communicate
Transfer from http://blog.csdn.net/hapylong/article/details/856564
Java Uploading Files 2