Struts---多文件上傳、單文件下載
阿新 • • 發佈:2017-08-11
conf doctype stack link posit onf for req image
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <constant name="struts.multipart.maxSize" value="3000000001000"></constant> 8 <package name="a" extends="struts-default"> 9 10 <!-- 上傳 多文件上傳 --> 11 <action name="upload" class="com.zr.fileupload" method="upload"> 12 <param name="allowedTypes">image/bmp,image.jpg</param> 13 <param name="allowedExtensions">.jpg</param> 14 <interceptor-ref name="fileUpload"> 15 <param name="maximumSize">81111110111</param> 16 </interceptor-ref> 17 <interceptor-ref name="defaultStack"/> 18 <result name="OK">/upload.jsp</result> 19 </action> 20 21 22 <!-- 下載 單文件下載 --> 23 <action name="DownLoad" class="com.zr.DownLoad"> 24 <result type="stream"> 25 <param name="contentDisposition">attachment;filename=${fileName}</param> 26 </result> 27 28 </action> 29 </package> 30 </struts>
下載方法
1 package com.zr; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 import java.io.UnsupportedEncodingException; 7 8 import com.opensymphony.xwork2.ActionSupport; 9 10 public class DownLoad extends ActionSupport{ 11 12 private String fileName; 13 14 public String getFileName() throws Exception{ 15 fileName=new String(fileName.getBytes(), "ISO8859-1"); 16 return fileName; 17 } 18 19 public void setFileName(String fileName) { 20 this.fileName = fileName; 21 } 22 23 public InputStream getInputStream()throws Exception{ 24 File file=new File("C:/jdk-6u39-windows-i586.exe"); 25 this.fileName="jdk-6u39-windows-i586.exe"; 26 return new FileInputStream(file); 27 } 28 29 }
實體類
1 package com.zr; 2 3 import java.io.File; 4 import java.io.IOException; 5 import org.apache.commons.io.FileUtils; 6 import com.opensymphony.xwork2.ActionSupport; 7 public class fileupload extends ActionSupport{ 8 9 private File [] upload; 10 private String [] uploadFileName; 11 private String [] uploadContentType; 12 13 14 public String upload() throws IOException{ 15 for (int i = 0; i <upload.length; i++) { 16 String filepath="c:/"+uploadFileName[i]; 17 File fp=new File(filepath); 18 FileUtils.copyFile(upload[i], fp); 19 } 20 return "OK"; 21 } 22 23 24 25 public File[] getUpload() { 26 return upload; 27 } 28 public void setUpload(File[] upload) { 29 this.upload = upload; 30 } 31 public String[] getUploadFileName() { 32 return uploadFileName; 33 } 34 public void setUploadFileName(String[] uploadFileName) { 35 this.uploadFileName = uploadFileName; 36 } 37 public String[] getUploadContentType() { 38 return uploadContentType; 39 } 40 public void setUploadContentType(String[] uploadContentType) { 41 this.uploadContentType = uploadContentType; 42 } 43 }
index.jsp頁面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 <SCRIPT type="text/javascript" src="jquery-1.8.2.min.js"></SCRIPT> 22 </head> 23 <body> 24 <embed 25 name="honehoneclock" 26 width="320" 27 align="middle" 28 pluginspage="http://www.macromedia.com/go/getflashplayer" 29 src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_wh.swf" 30 type="application/x-shockwave-flash" 31 allowscriptaccess="always" 32 bgcolor="#ffffff" 33 quality="high" 34 wmode="transparent" 35 > 36 37 <form action="upload" method="post" enctype="multipart/form-data"> 38 <input type="file" name="upload"><br/> 39 <input type="file" name="upload"><br/> 40 <input type="file" name="upload"><br/> 41 <input type="submit" value="上傳"> <br/> 42 </form> 43 44 <a href="DownLoad.action">下載</a> 45 46 </body> 47
48 </html>
struts配置註意事件
以上內容為個人筆記記錄
可供他人參考
2017-08-1112:51:01
Struts---多文件上傳、單文件下載