一、文件上传
1、客户端表单
<s:form action="upload" theme="simple" enctype="multipart/form-data">
<table align="center" border="1" width="50%">
<tr>
<td>
<s:file name="file" label="file1" id="fileid"></s:file>
</td>
</tr>
<tr>
<td>
<s:submit value="submit"></s:submit>
</td>
</tr>
</table>
</s:form>
2 、struts.xml配置
<action name="upload" class="com.test.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">409600</param>
<param name="allowedTypes">text/plain</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
3、action类
for (int i = 0; i < file.size(); i++) {
InputStream is = new FileInputStream(file.get(i));
String root = ServletActionContext.getRequest().getRealPath("/upload");
File destFile = new File(root, this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);}
is.close();
os.close();
}
二、文件下载
1、客户端表单
<s:a href="/MyStructs2/download.action">download</s:a>
2、struts.xml配置
<action name="download" class="com.test.action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">downloadFile</param>
<param name="contentDisposition">attachment;filename="linux.txt"</param>
<param name="bufferSize">1024</param>
</result>
</action>
3、action类实现getDownloadFile方法
public InputStream getDownloadFile()
{
return ServletActionContext.getServletContext().getResourceAsStream("/upload/linux3.txt");
}
三、测试一下吧!
分享到:
相关推荐
使用Struts2实现文件上传和下载的所有方案
在struts2基础上实现的包括单文件、多文件的上传及下载
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
struts实现的文件上传下载功能,最大可以上传到150MB
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
struts与hibernate实现文件的上传与动态下载 主要就是运用struts2.2和hibernate3.6进行文件上传与下载,并且能动态的下载,里面包括了详细的构建文档,以及代码
struts1实现文件上传和下载,struts1实现文件上传和下载。
Struts2实现文件上传的功能,简洁方便,欢迎下载。
struts1和struts2分别实现文件上传下载功能
struts2实现文件上传(单个+多个文件上传
struts2实现的文件上传下载,上传文件重命名(按时间戳)
自己做的一个struts2 实现文件批量上传功能,
注意:private String uploadContextType;应更正为private String uploadContentType;不过不影响正常运行
一个swfupload+struts2实现多文件上传的案例。
用struts实现文件的上传和下载,在实现中,对于文件大小大于3M的进行限定.(注意版本问题)
用struts1实现文件的上传和下载,在本机已测试成功,一个简单的实现文件上传和下载的功能
struts1 实现文件上传下载...
基于Struts2的文件上传下载功能的完整源代码。经过测试可正确运行。
java struts2实现文件上传进度条显示
Struts2 实现图片上传和文档下载,简单容易理解的小例子,带注释。