1,jsp Page Upload Control
<form action= "/easyuissh/model/upload.do" enctype= "Multipart/form-data" method= "POST" >
<input type= "File" name= "MyFile"/>
</form>
2,form Defining upload file types
Package com.platform_easyuiSSH.struts1.action.form;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.upload.FormFile;
public class Uploadform extends actionform{
/**
* Serial number equivalent to the class ID card, if you persist the operation, after the class has changed, in the persistence will error, but with the serial number, will persist to the hard disk class together change
*/
Private static final long serialversionuid = 1L;
Private Formfile myfile;
Public Formfile Getmyfile () {
return myfile;
}
public void Setmyfile (Formfile myfile) {
This.myfile = myfile;
}
}
3,action performing an upload operation
Package com.platform_easyuiSSH.struts1.action;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
Import Org.apache.struts.upload.FormFile;
Import Com.platform_easyuiSSH.struts1.action.form.UploadForm;
public class Uploadaction extends action{
@Override
Public Actionforward Execute (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
Uploadform uf = (uploadform) Form;
Formfile myfile = Uf.getmyfile ();
Filename
String fileName = Myfile.getfilename ();
File size
int fileSize = Myfile.getfilesize ();
File type
String FileType = Myfile.getcontenttype ();
SYSTEM.OUT.PRINTLN ("FileName:" +filename+ "\ t file size:" +filesize+ "\ t file type:" +filetype);
String Realpath = Request.getsession (). Getservletcontext (). Getrealpath ("/upload");
System.out.println (Realpath);
if (fileSize! = 0) {
Input stream receives file, byte array is too small for
InputStream is = Myfile.getinputstream ();
byte[] buf = new byte[1024];
OutputStream OS = new FileOutputStream (realpath+ "\ \" +filename);
while (true) {
int len = Is.read (BUF);
if (len = =-1) {
Break
}
Os.write (buf, 0, Len);
}
Os.close ();
Is.close ();
}
Return Mapping.findforward ("logoutsuccess");
}
}
struts1.x Upload