1、命名空间,设置即可,分模块即可
<package name="struts2" extends="struts-default" namespace="/yanlei">
建立目录文件夹 yanlei
<s:form action="yanlei/token.action" theme="simple">
注:但result路径还是必须 <result name="success">/yanlei/tokenSuccess.jsp</result>
2、 文件上传 :<form action="UploadServlet" method="post" enctype="multipart/form-data">
方法一:用commons-fileupload-1.2.2.jar 包,fileupload组件。。
DiskFileItemFactory <span style="color:#cc0000;">factory</span> = new DiskFileItemFactory(); //建立文件工厂,设置好位置及传输大小
String path = request.getRealPath("/upload");
<span style="color:#ff6600;">factory</span>.setRepository(new File(path));
<span style="color:#ff0000;">factory.</span>setSizeThreshold(1024 * 1024);
ServletFileUpload <span style="background-color: rgb(255, 153, 0);">upload</span> = new ServletFileUpload(<span style="color:#ff6666;">factory</span>); //servlet文件上传 设置为工厂模式
List<FileItem> list = <span style="background-color: rgb(255, 153, 102);">upload</span>.parseRequest(request); //servlet解析请求,得到一个List,包含文本、文件条图
for (FileItem item : list) { //遍历,是文本直接输出,是文件,输出文件名并上传
String name = item.getFieldName();
<span style="background-color: rgb(102, 255, 255);">if (item.isFormField()) { </span>
String value = item.getString();
System.out.println(name + "=" + value);
request.setAttribute(name, value);
<span style="background-color: rgb(0, 204, 204);">} else {</span>
String value = item.getName();
System.out.println(value);
int start = value.lastIndexOf("\\");
String fileName = value.substring(start + 1);
request.setAttribute(name, fileName);
item.write(new File(path, fileName));//<span style="background-color: rgb(204, 102, 204);">直接写到服务器,封装好的,但内部方法很多不能实现了。</span>
}
}
方法二:item.write(new File(path, fileName)); 封装了很多,我们可以自己用流 写。但write方法封装了删除临时文件。
<span style="white-space:pre"> </span>InputStream is = item.getInputStream();
OutputStream os = new FileOutputStream(new File(path,
fileName));
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.close();
is.close();
方法三:struts文件上传。显示将文件上传到 struts.properties所指定的struts.multipart.saveDir=D:\VDownload,然后再通过IO流存放到root中(分了两步)
(所以file.getName与fileFileName不同)
action中 //注意名字必须符合规定
private File file1;
private String file1FileName;
private String file1ContentType;
public String execute() throws Exception {
String root = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println(file1.getName());
System.out.println(file1FileName);
InputStream is = new FileInputStream(file1);
File destFile = new File(root, file1FileName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
Thread.sleep(1000);
}
os.close();
is.close();
return SUCCESS;
}
3,文件上传是通过 FileUploadInterceptor 拦截器来实现的,在默认的defaultStack 栈中,
可以通过传入在strutsproperties中 struts.multipart.maxSize=2097152 来设置全局的上传文件的大小。
也可以在struts.xml,用<constant name="struts.multipart.maxSize" value="11111"></constant> 来指定
4、文件下载
public class DownloadAction extends ActionSupport {
public InputStream get<span style="color:#3333ff;background-color: rgb(255, 255, 51);">DownloadFile</span>() {
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/mysql.txt"); //将服务器端的文件资源转化为流
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
struts.xml中,
<action name="downloadAction" class="com.yanlei.action.DownloadAction">
<result name="success" type="<span style="background-color: rgb(255, 0, 0);">stream</span>"> //返回的结果类型为 流的类型,而不是请求转发了
<param name="contentDisposition">attachment;filename="mysql.txt"</param>
//http 协议,<span style="font-size: 13.3333px; font-family: Arial, Helvetica, sans-serif;">attachment 保证每次跳出下载,txt文件有些浏览器可以直接打开。filename下载时候的名字,假名字。</span>
<param name="inputName">downloadFile</param> //必须是Action中那个方法,可以通过反射调用
</result>
</action>
注: <result type="stream">
<param name="contentDisposition">attachment;filename=${filename}</param>
<param name="inputName">downloadFile</param>
</result>
可以用 ${filename}接受传过来的 参数,这个参数在action中定义,赋值即可。
中文会出问题,可以在ACTION中this.filename = new String(this.filename.getBytes("gbk"),"8859_1"); 字符转个码即可。。