structs2之多文件上传
生活随笔
收集整理的這篇文章主要介紹了
structs2之多文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//首先是Action部分
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import java.util.List;import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext;import com.hzml.dao.DistributeDao; import com.opensymphony.xwork2.ActionSupport;public class CompanyAndDistributeAction extends ActionSupport{
//和form表單對應的name屬性private DistributeDao distributeDao;private String developName;private String email;private String phone;private String timeID;private String money;private String taskDescribe;
//多文件上傳,form中的每個文件的name屬性必須一樣,在這里我的name="file"private List<File> file; // 上傳的文件private List<String> fileFileName; // 文件名稱private List<String> fileContentType; // 文件類型private String savePath;public DistributeDao getDistributeDao() {return distributeDao;}public void setDistributeDao(DistributeDao distributeDao) {this.distributeDao = distributeDao;}public String getDevelopName() {return developName;}public void setDevelopName(String developName) {this.developName = developName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getTimeID() {return timeID;}public void setTimeID(String timeID) {this.timeID = timeID;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;}public String getTaskDescribe() {return taskDescribe;}public void setTaskDescribe(String taskDescribe) {this.taskDescribe = taskDescribe;}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;}public String getSavePath() {return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}private void init() throws UnsupportedEncodingException{ServletActionContext.getRequest().setCharacterEncoding("UTF-8");ServletContext context = ServletActionContext.getServletContext(); }public String saveTask () throws Exception{init();// 取得需要上傳的文件數組List<File> files = getFile();if (files != null && files.size() > 0) {for (int i = 0; i < files.size(); i++) {FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));FileInputStream fis = new FileInputStream(files.get(i));byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fis.close();fos.close();}}return "saveTask";}@Overridepublic String execute() throws Exception{return "success";} } //jsp代碼部分
<form action="companyAndDistributeAction" method="post" enctype="multipart/form-data" name="contactForm" id="contactForm"><br/><div><label>發布者:<span>*</span></label><input type="text" name="developName" id="developName" /></div><div><label>郵箱:<span>*</span></label><input type="text" name="email" id="email" /></div><div><label>電話:</label><input type="text" name="phone" id="phone" /></div><div><label>任務完成時間:<span>*</span></label><input type="text" name="timeID" id="timeID" /></div><div><label>任務費用:<span>*</span></label><input type="text" name="money" id="money" /></div><div><label>任務描述:</label><textarea name="taskDescribe" rows="10" cols="20" id="taskDescribe"></textarea></div><div><label>任務說明文檔:<span>*</span></label><input type="file" name="file" id="file" /></div><a class="button" href="#" style="float:right;" onclick="$('#contactForm')[0].submit(); return false;" id="send"><span>發布任務</span></a></form> //structs2配置文件部分
<?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><!-- 將Action的創建交給spring來管理 --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.i18n.encoding" value="utf-8"/><package namespace="/" name="struts2" extends="struts-default"> <!-- package中的標簽必須按照如下順序配置result-types,interceptors,default-interceptor-ref,default-action-ref,default-class-ref,global-results,global-exception-mappings,action*(就是所有的action放到最后)--><!-- 自定義攔截器 ,如果有攔截器,必須放在package標簽內的第一位--><interceptors><!-- 在這里可以添加自己的攔截器<interceptor name="myInterceptor" class="自定義pojo類"></interceptor>--><interceptor-stack name="myInterceptorStack"><!-- <interceptor-ref name="myInterceptor"></interceptor-ref>--><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors> <global-results><result></result></global-results> <action name="companyAndDistributeAction" class="companyAndDistributeAction" method="saveTask"><!-- 要創建/hzmlFile文件夾,否則會報找不到文件 --><param name="savePath">/hzmlFile</param><result name="saveTask">/ActionIntroduction.jsp</result> </action><!-- 訪問主頁 --><action name="visitMainPage" class="login" method="visitMainPage"><result name="visitMainPage">/index.jsp</result> </action><!-- 用戶登錄 --><action name="userLogin" class="login" method="userLogin"><result name="success">/ActionIntroduction.jsp</result> </action></package> </struts>
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import java.util.List;import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext;import com.hzml.dao.DistributeDao; import com.opensymphony.xwork2.ActionSupport;public class CompanyAndDistributeAction extends ActionSupport{
//和form表單對應的name屬性private DistributeDao distributeDao;private String developName;private String email;private String phone;private String timeID;private String money;private String taskDescribe;
//多文件上傳,form中的每個文件的name屬性必須一樣,在這里我的name="file"private List<File> file; // 上傳的文件private List<String> fileFileName; // 文件名稱private List<String> fileContentType; // 文件類型private String savePath;public DistributeDao getDistributeDao() {return distributeDao;}public void setDistributeDao(DistributeDao distributeDao) {this.distributeDao = distributeDao;}public String getDevelopName() {return developName;}public void setDevelopName(String developName) {this.developName = developName;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getTimeID() {return timeID;}public void setTimeID(String timeID) {this.timeID = timeID;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;}public String getTaskDescribe() {return taskDescribe;}public void setTaskDescribe(String taskDescribe) {this.taskDescribe = taskDescribe;}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;}public String getSavePath() {return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}private void init() throws UnsupportedEncodingException{ServletActionContext.getRequest().setCharacterEncoding("UTF-8");ServletContext context = ServletActionContext.getServletContext(); }public String saveTask () throws Exception{init();// 取得需要上傳的文件數組List<File> files = getFile();if (files != null && files.size() > 0) {for (int i = 0; i < files.size(); i++) {FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));FileInputStream fis = new FileInputStream(files.get(i));byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fis.close();fos.close();}}return "saveTask";}@Overridepublic String execute() throws Exception{return "success";} } //jsp代碼部分
<form action="companyAndDistributeAction" method="post" enctype="multipart/form-data" name="contactForm" id="contactForm"><br/><div><label>發布者:<span>*</span></label><input type="text" name="developName" id="developName" /></div><div><label>郵箱:<span>*</span></label><input type="text" name="email" id="email" /></div><div><label>電話:</label><input type="text" name="phone" id="phone" /></div><div><label>任務完成時間:<span>*</span></label><input type="text" name="timeID" id="timeID" /></div><div><label>任務費用:<span>*</span></label><input type="text" name="money" id="money" /></div><div><label>任務描述:</label><textarea name="taskDescribe" rows="10" cols="20" id="taskDescribe"></textarea></div><div><label>任務說明文檔:<span>*</span></label><input type="file" name="file" id="file" /></div><a class="button" href="#" style="float:right;" onclick="$('#contactForm')[0].submit(); return false;" id="send"><span>發布任務</span></a></form> //structs2配置文件部分
<?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><!-- 將Action的創建交給spring來管理 --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.i18n.encoding" value="utf-8"/><package namespace="/" name="struts2" extends="struts-default"> <!-- package中的標簽必須按照如下順序配置result-types,interceptors,default-interceptor-ref,default-action-ref,default-class-ref,global-results,global-exception-mappings,action*(就是所有的action放到最后)--><!-- 自定義攔截器 ,如果有攔截器,必須放在package標簽內的第一位--><interceptors><!-- 在這里可以添加自己的攔截器<interceptor name="myInterceptor" class="自定義pojo類"></interceptor>--><interceptor-stack name="myInterceptorStack"><!-- <interceptor-ref name="myInterceptor"></interceptor-ref>--><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors> <global-results><result></result></global-results> <action name="companyAndDistributeAction" class="companyAndDistributeAction" method="saveTask"><!-- 要創建/hzmlFile文件夾,否則會報找不到文件 --><param name="savePath">/hzmlFile</param><result name="saveTask">/ActionIntroduction.jsp</result> </action><!-- 訪問主頁 --><action name="visitMainPage" class="login" method="visitMainPage"><result name="visitMainPage">/index.jsp</result> </action><!-- 用戶登錄 --><action name="userLogin" class="login" method="userLogin"><result name="success">/ActionIntroduction.jsp</result> </action></package> </struts>
?
轉載于:https://www.cnblogs.com/hujunzheng/p/4574449.html
總結
以上是生活随笔為你收集整理的structs2之多文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 唾液酸纯化中水分如何快速去掉
- 下一篇: 李鹏园这个名字的含义