struts2上傳多個檔案,下載 配製!程式碼
頁面程式碼:
注意,我在頁面在一個新建的資料夾下(TheNameSpace)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'uplod.jsp' starting page</title>
</head>
<body>
<h1>file upload</h1>
<s:form action="uploadList.action" enctype="multipart/form-data" theme="simple">
<table cellpadding="0" height="5" >
<tr>
<td>使用者:</td>
<td>
<s:textfield name="username"/>
</td>
</tr>
<tr>
<td>密碼:</td>
<td> <s:password name="passwrod"/></td>
</tr>
<tr >
<td>檔案:</td>
<td id="fileID">
<s:fielderror name="file"/>
<s:file name="file" />
<a href="javascript:addFiles()" >新增上傳</a>
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="提交"/>
<s:reset value="重置"/>
</td>
</tr>
</table>
</s:form>
<script language="javascript">
function addFiles(){
var tdID=document.getElementById("fileID");
var br=document.createElement("br");
var file=document.createElement("input");
var button=document.createElement("input");
file.type="file";
file.name="file";
button.type="button";
button.value="remove";
button.onclick=function(){
tdID.removeChild(br);
tdID.removeChild(file);
tdID.removeChild(button);
}
tdID.appendChild(br);
tdID.appendChild(file);
tdID.appendChild(button);
}
</script>
</body>
</html>
struts.xml配製檔案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 常量 上傳檔案 最大大小 -->
<constant name="struts.multipart.maxSize" value="20971520"/>
<!-- 包括這個xml -->
<include file="strtus_1.xml"/>
<!-- 後臺action -->
<package name="back" extends="struts-default" namespace="/TheNameSpace">
<action name="logins" class="com.rui.struts2.SpaceLogin">
<result name="success">showUser.jsp</result>
</action>
<action name="uploadList" class="com.rui.struts.UploadList">
<result name="success">ok.jsp</result>
<result name="input">/${pageContext.request.contextPath}/TheNameSpace/uploadList.jsp</result>
<!--攔截上上傳檔案的 大小、格式 -->
<interceptor-ref name="fileUpload">
<param name="setAllowedTypes">image/pjpeg,image/gif,image/bmp,image/jpeg,image/jpg, text/plain, application/java-archive</param>
<param name="maximumSize">524288</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
</action>
<action name="upload" class="com.rui.struts.Upload">
<result name="success">ok.jsp</result>
<result name="input">upload.jsp</result>
</action>
<action name="download" class="com.rui.struts.DownLoald">
<result type="stream">
<!-- 要下載的檔案
<param name="contentDisposition">attachment;filename="bbbb.txt"</param>
-->
<!-- 自動尋找方法 -->
<param name="inputName">downloadFile</param>
</result>
</action>
<action name="showlist" class="com.rui.struts.ShowFileList">
<result>download.jsp</result>
</action>
</package>
</struts>
action 上傳類的程式碼:
package com.rui.struts;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadList extends ActionSupport {
private String username;
private String passwrod;
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswrod() {
return passwrod;
}
public void setPasswrod(String passwrod) {
this.passwrod = passwrod;
}
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public void validate() {
System.out.println("執行了驗證器...");
if(null==file){
addFieldError("file", "請選擇檔案!");
}
}
@Override
public String execute() throws Exception {
if(null==file||file.size()<=0){
addFieldError("file", "請上傳符合格式的檔案....");
return INPUT;
}
//獲得路麼
String path=ServletActionContext.getRequest().getRealPath("upload");
for(int i=0;i<file.size();i++){
//輸入流
InputStream is=new FileInputStream(file.get(i));
//File 物件
File fileObj=new File(path,fileFileName.get(i));
//輸出流
OutputStream os=new FileOutputStream(fileObj);
byte [] by=new byte[400];
int length=0;
while(-1!=(length=is.read(by))){
os.write(by,0,length);
}
os.close();
is.close();
}
return SUCCESS;
}
}
下載頁面測試 :
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'download.jsp' starting page</title>
</head>
<body>
<s:iterator value="listName" var="lname">
<s:property value="lname"/><br/><br/>
<a href="download.action?lname=<s:property value='#lname'/>" >下載檔案<a>
<!--
<a href="download.action?lname=DWHJ_062001.jpg">下載檔案</a>
-->
</s:iterator>
<br/><br/>
</body>
</html>
package com.rui.struts;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoald extends ActionSupport {
private String lname;
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
private String slname="" ;
public InputStream getDownloadFile(){
HttpServletResponse response=ServletActionContext.getResponse();
try {
slname=new String(lname.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
lname=java.net.URLEncoder.encode(slname,"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ddd"+lname);
response.setHeader("Content-Disposition","attachment;filename="+lname);
return ServletActionContext.getServletContext().
getResourceAsStream("upload/"+slname);
}
//檔名如果有中文的話要進行uri中文轉碼
/*String encodFileNmae="";
try {
encodFileNmae=java.net.URLEncoder.encode(u.getOldname(),"utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//設定一個請求頭告訴瀏覽器有檔案要下載
response.setContentType("text/html;charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename="+encodFileNmae);
*/
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
相關文章
- Struts2的檔案上傳下載
- 檔案上傳/下載後臺程式碼
- SpringMVC檔案上傳下載(單檔案、多檔案)SpringMVC
- 檔案上傳下載
- JAVA檔案上傳下載Java
- springboot 檔案上傳下載Spring Boot
- 檔案上傳與下載
- 使用SecureCRT上傳下載檔案Securecrt
- minio檔案上傳與下載
- 檔案的上傳與下載
- SpringMVC檔案上傳與下載(附工程原始碼)SpringMVC原始碼
- Netty接收HTTP檔案上傳及檔案下載NettyHTTP
- spring cloud feign 檔案上傳和檔案下載SpringCloud
- Feign實現檔案上傳下載
- java 上傳 下載檔案工具類Java
- 檔案下載上傳小工具
- 檔案上傳下載小工具
- Spring Boot 檔案上傳與下載Spring Boot
- xshell 使用 sftp上傳下載檔案FTP
- springcloud中feign檔案上傳、下載SpringGCCloud
- SpringMVC多個檔案上傳實現SpringMVC
- JavaWeb之實現檔案上傳與下載原始碼JavaWeb原始碼
- 關於檔案上傳下載的編碼問題
- Android WebView適配html載入本地檔案並上傳AndroidWebViewHTML
- SpringMVC實現檔案上傳&下載(2)SpringMVC
- springboot 中檔案的上傳和下載Spring Boot
- xshell安裝上傳下載檔案命令
- 【liunx命令】上傳下載檔案的方法
- Koa2 之檔案上傳下載
- laravel 多檔案上傳Laravel
- SpringMVC實現多檔案上傳原始碼SpringMVC原始碼
- Vue實現多檔案上傳功能(前端 + 後端程式碼)Vue前端後端
- 單個檔案上傳和批量檔案上傳
- 精講RestTemplate第6篇-檔案上傳下載與大檔案流式下載REST
- 使用Vue+go實現前後端檔案的上傳下載,csv檔案上傳下載可直接照搬VueGo後端
- 基於SpringWeb MultipartFile檔案上傳、下載功能SpringWeb
- 前端實現檔案下載和拖拽上傳前端
- 記一個 FormData 多檔案上傳問題ORM
- Java SE 檔案上傳和檔案下載的底層原理Java