Java struts2 upload and download

Source: Internet
Author: User

// Download
Public class downloadaction extends actionsupport {
Private Final Static string downloadfilepath = "/upload /";
Private string filename;

Public String getfilename (){
Return filename;
}
Public void setfilename (string filename ){
This. filename = filename;
}

Public inputstream getinputstream (){
Inputstream in;

In = servletactioncontext. getservletcontext (). getresourceasstream (downloadfilepath + filename );
System. Out. println (downloadfilepath + filename );
System. Out. println (in );
Return in;
}
Public String getdownloadchinesefilename (){
String downloadchinesefilename = filename;

Try {
Downloadchinesefilename = new string (downloadchinesefilename. getbytes (), "ISO8859-1 ");
System. Out. println (downloadchinesefilename );
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();

}
System. Out. println (downloadchinesefilename );
Return downloadchinesefilename;
}
@ Override
Public String execute (){
// Todo auto-generated method stub
Return success;
}
}

// Upload

Public class uploadaction extends actionsupport {
Private Final Static string uploaddir = "/upload ";
Private list <File> file;
Private list <string> filefilename;
Private list <string> filecontentype;
Public list <File> GetFile (){
Return file;
}
Public void setfile (list <File> file ){
This. File = file;
}
Public list <string> getfilefilename (){
Return filefilename;
}
Public void setfilefilename (list <string> filefilename ){
This. filefilename = filefilename;
}
Public list <string> getfilecontentype (){
Return filecontentype;
}
Public void setfilecontentype (list <string> filecontentype ){
This. filecontentype = filecontentype;
}

@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
For (INT I = 0; I <file. Size (); I ++ ){
Uploadfile (I );

}
Return "success ";
}
Private void uploadfile (int I) throws filenotfoundexception, ioexception {
Try {
Inputstream in = new fileinputstream (file. Get (I ));
String dir = servletactioncontext. getrequest (). getrealpath (uploaddir );
System. Out. println (DIR );
File uploadfile = new file (Dir, this. getfilefilename (). Get (I ));
Outputstream out = new fileoutputstream (uploadfile );

Byte [] buffer = new byte [104*1024];
Int length;
While (length = in. Read (buffer)> 0 ){
Out. Write (buffer, 0, length );
}

} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

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.