Use struts2 + jsp to upload files. Use struts2 to upload files.

Source: Internet
Author: User
Tags connection reset

Use struts2 + jsp to upload files. Use struts2 to upload files.

This document describes how to upload files using struts2 + jsp. Share it with you for your reference. The details are as follows:

1. java code:

Package com. wang. test; import java. io. inputStream; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.Action; import com. opensymphony. xwork2.ActionSupport; The public class DownLoadPhoneFile extends ActionSupport {// The getDownloadFile () method must return InputStream. The getResourceAsStream () method outputs the resource public InputStream getDownloadFile () {return ServletActionContext through a stream. getServletContext (). getResourceAsStream ("/upload/UserLogin_7.27.apk");} public String execute () {return Action. SUCCESS ;} /************ [Implementation of Struts2 File Download ]***************** *************************** // If you directly write a link chain to the desired download on the file, in some cases, it is automatically opened in the browser by default. // this situation is not conducive to file download and permission control. Therefore, we do not use this method when downloading files. // we use the standard HTTP protocol to output a binary stream, which causes the browser to recognize the stream, it downloads files again // In fact, this method is related to the output. When you click the download link, a download information is generated. It is result-related // so go to The struts-default.xml to view the <result-type/> result type // Where <result-type name = "stream" class = "org. apache. struts2.dispatcher. streamResult "/> // In fact, The StreamResult class is used to download a file, the console prompts a socket exception ]********************************** * /// the error message is java.net. socketException: Connection reset by peer: socket write error // download itself is also a socket operation, so this exception is thrown. In fact, this exception can be ignored. This Exception will be thrown every time you download it. // After throws Exception on the getDownloadFile () method, this exception information will no longer be reported on the console./************* [source code snippets used to process the StreamResult class for File Download ]**** * ************************** // The org. apache. struts2.dispatcher. source code snippet of the StreamResult class // public class StreamResult extends StrutsResultSupport {// protected String contentType = "text/plain"; // protected String contentLength; // protected String contentDisposition = "inline ";/ /Protected String inputName = "inputStream"; // protected InputStream inputStream; // protected int bufferSize = 1024; /************ [analyze the three important attributes of the StreamResult class ]**************** * *********************** // here we mainly focus on the three attributes of the StreamResult class.: contentType, contentDisposition, and inputName // all attributes are stored in struts. after the xml configuration, Struts2 is automatically injected into the object. // The contentType is used to specify the type of the downloaded object, contentDisposition is used to specify the name of the downloaded object // In addition, bufferSize is used to set the download speed The size of the punch area. The default value is 1 kb, which is usually 1 kb by default. // In fact, these attributes are obtained based on the HTTP protocol. The HTTP Protocol specifies that these attributes must be used when downloading files. // The most critical attribute is the protected String inputName attribute, it is used to specify the IO stream of the actually downloaded file. // Therefore, an input stream must be returned in DownloadAction. Because when downloading, it is an operation to input files from the server /*************************** **************************************** ********************/}

2. the xml Code is as follows:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>  <package name="default" extends="struts-default" namespace="/">    <action name="download" class="com.wang.test.DownLoadPhoneFile">      <result name="success" type="stream">        <param name="contentType">application/vnd.ms-powerpoint</param>        <param name="contentDisposition">attachment;filename="UserLogin_7.27.apk"</param>        <param name="inputName">downloadFile</param>      </result>    </action>  </package></struts>

3. the JSP page code is as follows:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

I hope this article will help you with JSP program design.

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.