Struts uploads and downloads

Source: Internet
Author: User
Tags array example

Strutsfileupload

Simple Example of File Upload
Html
The HTML page needs to do two things. First, the form needs to specify enctype = "multipart/form-dataand", and then a <input> Form Control of the file type.

<Form name = "myform" method = "Post" Action = "/mywebapp/uploadmyfile. Do" enctype = "multipart/form-Data">

Select File: <input type = "file" name = "myfile"> </BR>

<Input type = "Submit" value = "Upload File">

</Form>

JSP
The preceding HTML Tag is replaced by the struts tag by the following code:

<HTML: Form Action = "/uploadmyfile. Do" enctype = "multipart/form-Data">

Select File: <HTML: file property = "myfile"> </BR>

<HTML: Submit value = "Upload File"/>

</Html: Form>

Actionform
This actionform requires a formfile field.

Normal actionform
Import org. Apache. Struts. Upload. formfile;

Public class myactionform extends actionform {

Private formfile myfile;

Public void setmyfile (formfile myfile ){
This. myfile = myfile;
}

Public formfile getmyfile (){
Return myfile;
}
}

Dynamic actionforms
Write in the struts-config.xml file:

<Form-bean name = "myform" type = "org. Apache. Struts. Action. dynaactionform">
<Form-property name = "myfile" type = "org. Apache. Struts. Upload. formfile"/>
</Form-bean>

What should I do in action?
In fact, there is nothing special. Just like getting other attributes, you can get the formfile attribute from actionform and process it at will. For example, we can get the file name, file size, and file content from fileform.

Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response) throws exception {

Myactionform myform = (myactionform) form;

// Process the formfile
Formfile myfile = myform. getmyfile ();
String contenttype = myfile. getcontenttype ();
String filename = myfile. getfilename ();
Int filesize = myfile. getfilesize ();
Byte [] filedata = myfile. getfiledata ();
...
}

File Upload Configuration
In the struts-config.xml's <controller> element, you can set the following parameters to configure file upload:

Buffersize-buffer size for processing file uploads, in bytes.
The default value is 4096 bytes.

Maxfilesize-the size of the file to be uploaded. The Unit is K, M, and G.
The default value is 250 MB.

The global identifier of the multipartclass-muiltpart request processor class. The default value is org. Apache. Struts. Upload. commonsmultipartrequesthandler.

Tempdir-temporary directory for processing file uploads.

There is also an optional File Upload plug-in that can be used to achieve
Org. Apache. Struts. Upload. multipartrequesthandler interface.
You can go to the <controller> multipartclass in the struts-config.xml
To specify the class for the interface.
==========================================
Strutsfiledownload

Struts 1.2.6 introduces a new downloadaction to simplify the download operation.

Downloadaction
We need to extend org. Apache. Struts. Actions. downloadaction and implement
Getstreaminfo () method. If we want to change the default buffer size, we can also Overwrite
Getbuffersize () method.

Implement the getstreaminfo () method
The getstreaminfo () method returns a streaminfo object-inside the downloadaction class
Class is actually an internal interface. Downloadaction provides two static
Implementation class:

Filestreaminfo-Simplified file downloads from the disk system. You need to input a java. Io. File object along with content type to the constructor.

Resourcestreaminfo-Simplified File Download from web application resources. You need to input the servletcontext, path, and content type to its constructor.

In the following example, we also provide a byte array method to implement the streaminfo interface generation.
.

Implement the getbuffersize () method
By default, downloadaction returns the 4096byte buffer. We can overwrite this method.
Buffer size for transferring files

Example
The following are three examples:

Use Files

Use web application resources

Use byte array

Filestreaminfo example
Example of downloadaction using a file. This example is from the action of the struts-config.xml
The parameter property of mapping to get the file name.

Import java. Io. file;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Actions. downloadaction;

Public class examplefiledownload extends downloadaction {

Protected streaminfo getstreaminfo (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

// Download a "pdf" file-gets the file name from
// Action mapping's parameter
String contenttype = "application/pdf ";
File file = new file (mapping. getparameter ());

Return new filestreaminfo (contenttype, file );

}

}

Resourcestreaminfo example
Downloadaction examples of using web application resources. This example from struts-config.xml's
The parameter attribute of Action mapping to obtain the path of the Web application resource.

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Actions. downloadaction;

Public class exampleresourcedownload extends downloadaction {

Protected streaminfo getstreaminfo (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

// Download a "Jpeg" file-gets the file name from
// Action mapping's parameter
String contenttype = "image/JPEG ";
String Path = mapping. getparameter ();
Servletcontext application = servlet. getservletcontext ();


Return new resourcestreaminfo (contenttype, application, PATH );

}

}

Byte array example
Downloadaction uses the byte array example.

In this example, an internal class of bytearraystreaminfo that implements the streaminfo interface is created.

Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. bytearrayinputstream;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Actions. downloadaction;

Public class examplebytearraydownload extends downloadaction {

Protected streaminfo getstreaminfo (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

// Download a "pdf" File
String contenttype = "application/pdf ";
Byte [] mypdfbytes = NULL; // get the bytes from somewhere
Return new bytearraystreaminfo (contenttype, mypdfbytes );

}

Protected class bytearraystreaminfo implements streaminfo {

Protected string contenttype;
Protected byte [] bytes;

Public bytearraystreaminfo (string contenttype, byte [] bytes ){
This. contenttype = contenttype;
This. bytes = bytes;
}

Public String getcontenttype (){
Return contenttype;
}

Public inputstream getinputstream () throws ioexception {
Return new bytearrayinputstream (bytes );
}
}
}

Use downloadaction on the web page
My biggest question is, how can I use this action?

Two things need to be done:

Like any struts action, you need to configure it in the struts-config.xml.

Use it to connect files on Web pages

The following is an example of a struts-config.xml Configuration:

<Action Path = "/downloadmypdffile" type = "mypackage. examplefiledownload" parameter = "/Foo/bareter">
<Action Path = "/downloadmyimage" type = "mypackage. exampleresourcedownload" parameter = "/images/myimage.jpeg">

The following example can be used on our JSP page:

<HTML: IMG action = "downloadmyimage" alt = "my image" Height = "400" width = "400"/>

<HTML: link action = "downloadmypdffile"> Click here to see the PDF

Note: We may want to set the nocache value of the <controller> attribute in the struts configuration file to false. If it is set to true, the file may not be successfully downloaded on IE, but it works normally on Firefox and Safari.

<Controller contenttype = "text/html; charsets = UTF-8" locale = "true" nocache = "false"/>

Content Disposition)
Set Content Disposition
Downloadaction cannot process the content Dispositon header. The simplest method is to set it in the getstreaminfo () method, for example:

Public class examplefiledownload extends downloadaction {

Protected streaminfo getstreaminfo (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws exception {

// File name
String filename = mapping. getparameter ();

// Set the Content Disposition
Response. setheader ("content-disposition ",
"Attachment; filename =" + filename );

// Download a "pdf" file-gets the file name from
// Action mapping's parameter
String contenttype = "application/pdf ";
File file = new file (filename );

Return new filestreaminfo (contenttype, file );

}
}

If you need a file name as a parameter, you may need to first clear any path information before the file.

Content Disposition Value
We can set content disposition to download a file or open a file in a browser.

Example of opening a file in a browser: "inline; filename=myfile="

Example: "attachment; filename=myfile="

To display an image, you can use the "inline" option of content disposition.

Trackback: http://tb.donews.net/TrackBack.aspx? Postid = 644250

 

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.