本文實(shí)例講述了struts2+jsp實(shí)現(xiàn)文件上傳的方法。分享給大家供大家參考。具體如下:
1. java代碼:
package com.wang.test;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadPhoneFile extends ActionSupport
{
////getDownloadFile()方法返回的必須是InputStream。getResourceAsStream()方法可以通過(guò)流的方式將資源輸出
public InputStream getDownloadFile()
{
return ServletActionContext.getServletContext().getResourceAsStream("/upload/UserLogin_7.27.apk");
}
public String execute()
{
return Action.SUCCESS;
}
/*************【Struts2的文件下載的實(shí)現(xiàn)方式】*********************************************/
//如果直接寫(xiě)一個(gè)鏈接鏈到所要下載的文件上的話,對(duì)于有的時(shí)候,默認(rèn)的會(huì)自動(dòng)在瀏覽器里面打開(kāi)
//這種情況非常不利于我們的文件下載和權(quán)限控制。因此,我們實(shí)現(xiàn)文件下載時(shí)都不會(huì)采用這種方式
//我們所采用的是標(biāo)準(zhǔn)HTTP協(xié)議的方式,輸出二進(jìn)制的流,導(dǎo)致瀏覽器認(rèn)識(shí)這個(gè)流,它再進(jìn)行文件下載
//實(shí)際上這種方式是跟輸出有關(guān)的,當(dāng)點(diǎn)擊下載鏈接時(shí),會(huì)產(chǎn)生下載的一個(gè)信息。它是跟result有關(guān)的
//所以就到struts-default.xml中查看result-type/>結(jié)果類型
//其中result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
//事實(shí)上,這里的StreamResult類是專門用來(lái)執(zhí)行文件下載的
/*************【每一次下載文件時(shí),控制臺(tái)都會(huì)提示socket異常】***********************************/
//報(bào)錯(cuò)信息為java.net.SocketException:Connection reset by peer: socket write error
//下載本身也是socket操作,于是拋出該異常。實(shí)際上這個(gè)異常可以忽略掉。每次下載的時(shí)候,都會(huì)拋出該異常
//在getDownloadFile()方法上throws Exception之后,控制臺(tái)上就不會(huì)再報(bào)告這個(gè)異常信息啦
/*************【用于處理文件下載的StreamResult類的源代碼片段】********************************/
//這里顯示的是org.apache.struts2.dispatcher.StreamResult類的源代碼片段
//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;
/*************【淺析StreamResult類的三個(gè)重要屬性】******************************************/
//這里我們主要關(guān)注一下StreamResult類的三個(gè)屬性:contentType、contentDisposition、inputName
//這些屬性都是通過(guò)在struts.xml配置之后,由Struts2自動(dòng)注入到對(duì)象里面去的
//其中contentType用來(lái)指定下載的文件的類型,contentDisposition用來(lái)指定下載文件的名字
//另外bufferSize用來(lái)設(shè)定下載文件時(shí)的緩沖區(qū)大小,默認(rèn)為1KB,通常按照默認(rèn)的1KB就可以了
//實(shí)際上這些屬性完全是根據(jù)HTTP協(xié)議得來(lái)的。HTTP協(xié)議就規(guī)定了下載文件的時(shí)候,需要使用到這些屬性
//其中最關(guān)鍵的就是protected String inputName屬性,它是用來(lái)指定真正下載的文件的IO流
//因此DownloadAction中必須返回一個(gè)輸入流。因?yàn)橄螺d的時(shí)候,本身就是一個(gè)從服務(wù)器端將文件輸入過(guò)來(lái)的操作
/***************************************************************************************/
}
2. xml代碼如下:
?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. JSP頁(yè)面代碼如下:
%@ 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">
html>
head>
base href="%=basePath%>">
title>My JSP 'index.jsp' starting page/title>
meta http-equiv="pragma" content="no-cache">
meta http-equiv="cache-control" content="no-cache">
meta http-equiv="expires" content="0">
meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
meta http-equiv="description" content="This is my page">
/head>
body>
input type="button" value="手機(jī)端安裝包下載" onclick="javascript:window.location='download.action';"/>
/body>
/html>
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- struts2實(shí)現(xiàn)多文件上傳
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- 關(guān)于Struts2文件上傳與自定義攔截器
- Struts2+jquery.form.js實(shí)現(xiàn)圖片與文件上傳的方法
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)簡(jiǎn)單的文件上傳與下載
- struts2實(shí)現(xiàn)多文件上傳的示例代碼
- JS+Struts2多文件上傳實(shí)例詳解