struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<constant name="struts.multipart.maxSize" value="3000000001000"></constant>
<package name="a" extends="struts-default"> <!-- 上传 多文件上传 -->
<action name="upload" class="com.zr.fileupload" method="upload">
<param name="allowedTypes">image/bmp,image.jpg</param>
<param name="allowedExtensions">.jpg</param>
<interceptor-ref name="fileUpload">
<param name="maximumSize">81111110111</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result name="OK">/upload.jsp</result>
</action> <!-- 下载 单文件下载 -->
<action name="DownLoad" class="com.zr.DownLoad">
<result type="stream">
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result> </action>
</package>
</struts>

下载方法

 package com.zr;

 import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import com.opensymphony.xwork2.ActionSupport; public class DownLoad extends ActionSupport{ private String fileName; public String getFileName() throws Exception{
fileName=new String(fileName.getBytes(), "ISO8859-1");
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInputStream()throws Exception{
File file=new File("C:/jdk-6u39-windows-i586.exe");
this.fileName="jdk-6u39-windows-i586.exe";
return new FileInputStream(file);
} }

实体类

 package com.zr;

 import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class fileupload extends ActionSupport{ private File [] upload;
private String [] uploadFileName;
private String [] uploadContentType; public String upload() throws IOException{
for (int i = 0; i <upload.length; i++) {
String filepath="c:/"+uploadFileName[i];
File fp=new File(filepath);
FileUtils.copyFile(upload[i], fp);
}
return "OK";
} public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
}

index.jsp页面

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<SCRIPT type="text/javascript" src="jquery-1.8.2.min.js"></SCRIPT>
</head>
<body>
<embed
name="honehoneclock"
width="320"
align="middle"
pluginspage="http://www.macromedia.com/go/getflashplayer"
src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_wh.swf"
type="application/x-shockwave-flash"
allowscriptaccess="always"
bgcolor="#ffffff"
quality="high"
wmode="transparent"
> <form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="upload"><br/>
<input type="file" name="upload"><br/>
<input type="file" name="upload"><br/>
<input type="submit" value="上传"> <br/>
</form> <a href="DownLoad.action">下载</a> </body>

</html>

struts配置注意事件

Struts---多文件上传、单文件下载-LMLPHP

以上内容为个人笔记记录

可供他人参考

2017-08-1112:51:01

05-27 21:12