------------->>转载于;http://blog.csdn.net/zjw10wei321/article/details/9031417
对于struts2一直都是看过,很少去动手写里面的功能,今天花了一点时间算整理整理。
整体的项目大致如下:
1:文件上传
uploadAction.java
上传页面upload.jsp:
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@taglib prefix="s" uri="/struts-tags" %>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
>
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>上传文件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>
-
<s:form action="uploads" method="post" enctype="multipart/form-data">
-
<s:textfield id="title" name="title" label="文件标题"/>
-
<s:file name="file" label="上传文件"/>
-
<s:submit value="提交"/>
-
s:form>
-
-
body>
-
html>
上传成功uploadSuccess.jsp:
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@taglib prefix="s" uri="/struts-tags" %>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
-
>
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'index.jsp' starting pagetitle>
-
<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>
-
上传文件:<s:property value="title" /><br/>
-
文件内容:
-
<s:if test="%{fileContentType.indexOf('image')>=0}">
-
<img src="<s:property value="'upload/'+fileFileName"/>"/>
-
s:if>
-
<s:else>
-
<s:property value="contents"/>
-
s:else>
-
body>
-
html>
主要的struts.xml:
-
xml version="1.0" encoding="UTF-8" ?>
-
>
-
<struts>
-
<constant name="struts.devMode" value="true" />
-
<constant name="struts.i18n.encoding" value="UTF-8" />
-
-
<package name="myDemo" extends="struts-default" namespace="/">
-
-
<action name="uploads" class="action.UploadAction" >
-
<param name="savePath">/uploadparam>
-
<result name="input">/upload.jspresult>
-
<result name="success">/uploadSuccess.jspresult>
-
action>
-
<action name="downloads" class="action.DownloadAction">
-
<param name="inputPath">uploadparam>
-
<result name="input">/downloadSuccess.jspresult>
-
<result name="success" type="stream">
-
<param name="contentType">application/octet-stream;charset=ISO8859-1 param>
-
<param name="inputName">downloadFileparam>
-
<param name="contentDisposition">attachment;filename="${downloadFileName}"param>
-
<param name="bufferSize">4096param>
-
result>
-
action>
-
-
<action name="download" class="action.DownloadAction" method="download">
-
<param name="inputPath">uploadparam>
-
<result name="success" >/download.jsp
-
result>
-
action>
-
-
<!-- <action name="*">
-
<result>/{1}.jspresult>
-
action> -->
-
package>
-
struts>
对于上传主要在action哩,主要有3个属性:***、***FileName、***ContentTyp。我的例子**为file。
成功后,页面若是图片形式,我把它显示出来,如果不是就显示里面的内容。
2:文件下载
文件下载主要配置在struts.xml里。
一般使用result=“stream” 在struts中进行文件下载
有4个参数:contentType(主要)、contentDisposition(主要)、inputName、bufferSize,相对于这2个主要些。
详细的可以参考这个页面:强烈碰击
常常也会在下载的时候碰到一个异常:Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the tag specified for this action.
主要为2中情况:
第一:文件路径不对,没有取到文件,只需要在return之前,把流放在system.out.println()中输出一下,是null的话,就是路径不对。
第二:action中需要配置""后面属性的那个get方法。
下载action:downloadAction.java
-
package action;
-
-
import java.io.File;
-
import java.io.InputStream;
-
import java.io.UnsupportedEncodingException;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.apache.struts2.ServletActionContext;
-
-
import com.opensymphony.xwork2.ActionSupport;
-
-
public class DownloadAction extends ActionSupport {
-
-
private static final long serialVersionUID = -5772950074616179246L;
-
-
private String fileName;
-
-
private String inputPath;
-
-
-
private List fileNames;
-
-
public String getFileName() {
-
return fileName;
-
}
-
-
public void setFileName(String fileName) {
-
-
try {
-
fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
-
} catch (UnsupportedEncodingException e) {
-
e.printStackTrace();
-
}
-
-
this.fileName = fileName;
-
}
-
-
public String getInputPath() {
-
return inputPath;
-
}
-
-
public void setInputPath(String inputPath) {
-
this.inputPath = inputPath;
-
}
-
-
public InputStream getDownloadFile() {
-
String urll = File.separator + inputPath + File.separator + fileName;
-
InputStream is = ServletActionContext.getServletContext()
-
.getResourceAsStream(urll);
-
-
return is;
-
}
-
-
public String getDownloadFileName() {
-
String downFileName = fileName;
-
-
try {
-
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
-
} catch (UnsupportedEncodingException e) {
-
e.printStackTrace();
-
}
-
-
return downFileName;
-
-
}
-
-
public String download() throws Exception {
-
String downloadDir = ServletActionContext.getServletContext()
-
.getRealPath(File.separator + inputPath);
-
File rootFile = new File(downloadDir);
-
File[] files = rootFile.listFiles();
-
fileNames = new ArrayList();
-
for (File f : files) {
-
if (!f.isDirectory()) {
-
String name = f.getName();
-
-
fileNames.add(name);
-
}
-
}
-
return SUCCESS;
-
}
-
-
public List getFileNames() {
-
return fileNames;
-
}
-
-
public void setFileNames(List fileNames) {
-
this.fileNames = fileNames;
-
}
-
-
}
下载页面download.jsp:
-
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
-
<%@ taglib prefix="s" uri="/struts-tags"%>
-
>
-
<html>
-
<head>
-
<title>My JSP 'index.jsp' starting pagetitle>
-
<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>
-
<font color="blue">--------------------------font>
-
<br>
-
-
<s:iterator value="fileNames" var="file">
-
<s:set value="file" id="files" name="files" scope="Request"/>
-
<%
-
String fname = (String)request.getAttribute("files");
-
fname = java.net.URLEncoder.encode(fname, "UTF-8");
-
%>
-
<a href="downloads.action?fileName=<%=fname %>">
-
<s:property value="file" />下载
-
a>
-
<br />
-
s:iterator>
-
body>
-
html>
3个乱码的地方:
a:前台中文,后台获取:
-
public void setFileName(String fileName) {
-
-
try {
-
fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
-
} catch (UnsupportedEncodingException e) {
-
e.printStackTrace();
-
}
-
-
this.fileName = fileName;
-
}
在未URLEncoder之前:
b:下载时显示中文名字:
-
public String getDownloadFileName() {
-
String downFileName = fileName;
-
-
try {
-
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
-
} catch (UnsupportedEncodingException e) {
-
e.printStackTrace();
-
}
-
-
return downFileName;
-
-
}
-
<action name="downloads" class="action.DownloadAction">
-
<param name="inputPath">uploadparam>
-
<result name="input">/downloadSuccess.jspresult>
-
<result name="success" type="stream">
-
<param name="contentType">application/octet-stream;charset=ISO8859-1 param>
-
<param name="inputName">downloadFileparam>
-
<param name="contentDisposition">attachment;filename="${downloadFileName}"param>
-
<param name="bufferSize">4096param>
-
result>
-
action>
主要为action配置时配置contentDisposition参数,attachment;filename="${downloadFileName}",
取的就为getDownloadFileName()的值。
c:链接中的中文编码
-
<s:iterator value="fileNames" var="file">
-
<s:set value="file" id="files" name="files" scope="Request"/>
-
<%
-
String fname = (String)request.getAttribute("files");
-
fname = java.net.URLEncoder.encode(fname, "UTF-8");
-
%>
-
<a href="downloads.action?fileName=<%=fname %>">
-
<s:property value="file" />下载
-
a>
-
<br />
-
s:iterator>
-

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29119536/viewspace-1175513/
,如需转载,请注明出处,否则将追究法律责任。