File upload and download ----> struts

Source: Internet
Author: User

Set the response type at the front end when downloading: Response. setcontenttype ("application/X-download ");
OS = response. getoutputstream ();
Is = new fileinputstream (new file (PATH ));

Set the frontend type when uploading: Response. setcontenttype ("text/html; charset = gb2312 ");
Fileoutputstream Fos = new fileoutputstream (tempfile + filepath + "\" + filename );
FOS. Write (fileitem. Get ());

Garbled conversion:

Encodefilter class implements the filter interface under the servlet package
In the dofilter method:
Request. setcharacterencoding ("UTF-8"); // transfers requests in UTF-8 form
Chain. dofilter (request, response); // allow requests

In action:
Uploadform = (uploadform) form;
Formfile photo = uploadform. getphoto (); // formfile is the file type defined in formbean.

Oracle functions:

Nvl (A, B): If a is notNullReturns a; otherwise, returns B.

Nvl (a, B, c): If a is notNullB is returned; otherwise, C is returned.

Decode (value, if1, then1, if2, then2, if3, then3 ,..., Else) indicates that if value is equal to if1, the result is then1...

Select instr ("yuechaotianyuechao", "Ao",-) position from dual; (-1 is the start position, 1 is the number of times)-17

Substr ("ABCDE",-6, 5) = NULL-6 is the string length starting from 6th to the left. 5 is the string length.

Substr ("ABCDE",-6th) = ABCDE-6 indicates that the string length is obtained from locations to the left.

MoD (A, B): remainder of A and B

Row_number () over (partition... Order ...)

Uploadform = (uploadform) form;
Formfile photo = uploadform. getphoto ();
Response. setcontenttype ("text/html; charset = UTF-8"); // set the output content format
String filename = photo. getfilename ();
Inputstream contentstream = photo. getinputstream ();
String savedir = This. getservlet (). getservletcontext (). getrealpath ("/");
File filephoto = new file (savedir, filename );
Fileoutputstream Fos = new fileoutputstream (filephoto );
/**
Int Len = 0;
Byte [] Buf = new byte [1024]; // create a 1024-byte array
While (LEN = contentstream. Read (BUF ))! =-1) // if you read the content
{
FOS. Write (BUF, 0, Len); // output the read subscript from 0 to Len length to the file output stream.
}
*/
FOS. Write (uploadform. getphoto (). getfiledata (); // replace the above rows
Contentstream. Close (); // close the stream

FOS. Close (); // close the stream

Step 1: Compile the page file

<% @ Page Language ="Java"Pageencoding =" UTF-8 "%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<% @ Includefile = "/header. jsp" %>
<Title>StrutsUpload a file </title>
</Head>
<Body>
<H2> UseStrutsUpload a file </H2>
<HR/>
<Form. Action = "<% = basepath %>Strutsfileupload. do? Method = upload"Method ="Post"
Enctype ="Multipart/form-Data">
<Table>
<Tr>
<TD> select the file to be uploaded </TD>
<TD>
<Input type ="File"Name =" filepath "size =" 20 "/> <br/>
</TD>
</Tr>
<Tr>
<TD>
<Input type = "Submit" value = "Upload"/>
</TD>
<TD>
<A href = "<% = basepath %> strutsfileupload. do? Method = download & Path = fileload/xxxx.txt "> I want to download </a>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
Step 2:Struts-Config. xml file, including form-bean and action configuration

<? XML version = "1.0" encoding = "UTF-8"?>
<! DoctypeStruts-Config public "-// Apache Software Foundation // DTD
StrutsConfiguration 1.2 // en "" http ://Struts.Apache.org/dtds/Struts-Config_1_2.dtd ">
<Data-sources/>
<Form-beans>
<Form-bean name = "fileuploadform"
Type = "com. xinglongjian.Struts. Fileupload. fileuploadform "/>
</Form-beans>
<Global-exceptions/>
<Global-forwards>
<Forward name = "error" Path = "/error/syserror. jsp"> </forward>
</Global-forwards>
<Action-mappings>
<Action Path = "/strutsfileupload" parameter = "method" name = "fileuploadform"
Type = "com. xinglongjian.Struts. Fileupload. fileuploadaction ">
<Forward name = "success" Path = "/Struts/Success. jsp "> </forward>
</Action>
</Action-mappings>
</Struts-Config>

Step 3: fileuploadform. Java

Public class fileuploadform. extends actionform
{
Private formfile filepath; // The type is formfile. The variable filepath is the same as the file name on the page.

Public formfile getfilepath ()
{
Return filepath;
}

Public void setfilepath (formfile filepath)
{
This. filepath = filepath;
}
 
}

Step 4: fileuploadaction. Java

Public class fileuploadaction extends dispatchaction
{
/**
* Upload a file
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return
* @ Throws exception
*/
Public actionforward upload (actionmapping mapping, actionform. Form, httpservletrequest request, httpservletresponse response) throws exception
{
Fileuploadform. fileform = (fileuploadform) form;
Formfile file = fileform. getfilepath ();
String realpath = This. getservlet (). getservletcontext (). getrealpath ("/fileload ");
If (file = NULL)
Return Mapping. findforward ("error ");
String filename = file. getfilename ();
Int size = file. getfilesize ();
If (size> 1024*1024)
Return Mapping. findforward ("error ");
Inputstream is = NULL;
Bufferedoutputstream BS = NULL;
Try
{
// Get a inputstream object form. uploadfile
Is = file. getinputstream ();
BS = new bufferedoutputstream (New fileoutputstream (realpath + "/" + filename ));
Byte [] buffer = new byte [20480];
Int COUNT = 0;
While (COUNT = is. Read ())! =-1)
BS. Write (buffer, 0, count );
}
Catch (exception E)
{
E. printstacktrace ();
}
Finally
{
BS. Flush ();
BS. Close ();
Is. Close ();
}

Return Mapping. findforward ("success ");
}
/**
* Download an object
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return
* @ Throws exception
*/
Public actionforward download (actionmapping mapping, actionform. Form, httpservletrequest request, httpservletresponse response) throws exception
{
String Path = request. getparameter ("path ");
String realpath = This. getservlet (). getservletcontext (). getrealpath ("/" + path );

File uploadfile = new file (realpath );
Inputstream is = new fileinputstream (uploadfile );
Outputstream S = response. getoutputstream ();

Bufferedinputstream Bis = new bufferedinputstream (is );
Bufferedoutputstream Bos = new bufferedoutputstream (OS );
//Code
Response. setheader ("content-disposition", "attachment; filename =" + urlencoder. encode (path, "UTF-8 "));
Int bytesread = 0;
Byte [] buffer = new byte [8192];
While (bytesread = bis. Read (buffer, 0, 8192 ))! =-1)
Bos. Write (buffer, 0, bytesread );
Bos. Close ();
Is. Close ();
Bis. Close ();
OS. Close ();
Return NULL;
}
}

Related Article

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.