struts1.x Upload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.