java Struts2框架下完成文件上傳功效。本站提示廣大學習愛好者:(java Struts2框架下完成文件上傳功效)文章只能為提供參考,不一定能成為您想要的結果。以下是java Struts2框架下完成文件上傳功效正文
本文實例為年夜家分享了Struts2框架完成文件上傳的辦法,供年夜家參考,詳細內容以下
struts2的設置裝備擺設進程
(1)在項目中參加jar包
(2)web.xml中filter(過濾器)的設置裝備擺設
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(3)struts.xml設置裝備擺設文件的編寫
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(4)action類的編寫
package com.xmgc.sc.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
public class MyUpLoadAction {
private String title;
private File upload;//與form表單中的file文件類型的名字是一樣的
private String uploadContentType;//前綴必需如果下面的upload
private String uploadFileName;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
/* public String getSavePath() {
//ServletContext cxt=ServletActionContext.getServletContext();
//String path=cxt.getRealPath("/");
//這個獲得的path為:http://localhost:8080/sc
//上傳今後變成E:\software\apache-tomcat-6.0.45\webapps\sc
return savePath;
}
public void setSavePath(String savePath) {
//E:\software\apache-tomcat-6.0.45\webapps\sc/myupload
this.savePath = ServletActionContext.getServletContext().getRealPath("/myupload");
}*/
public String execute() throws IOException{
System.out.println(title);//題目
System.out.println(uploadContentType);//預備上傳的文件的文件類型
System.out.println(uploadFileName);//預備上傳的文件的文件名,記住還有擴大名
System.out.println(upload);
String realPath=ServletActionContext.getServletContext().getRealPath("/");
String path=realPath+"myupload/"+uploadFileName;
System.out.println(realPath);
System.out.println(path);
FileInputStream fis=new FileInputStream(upload);
FileOutputStream fos=new FileOutputStream(path);
byte[] bytes=new byte[1024];//界說一個1024年夜小的字節數組
int len=-1;//用來做標記位
while((len=fis.read(bytes))>0){
fos.write(bytes, 0, len);
}
return null;
}
}
(5)JSP頁面的編寫
<%@ page contentType="text/html;charset=utf-8"%>
<!-- enctype="multipart/form-data",這個是最主要的設置裝備擺設 -->
<form action="myupload.action" enctype="multipart/form-data" method="post">
文件名:<input type="text" name="title"/><br/>
上傳:<input type="file" name="upload"/><br/>
<input type="submit" value="上傳"/>
</form>
經由此次總結,感到struts2框架下的單文件的上傳照樣異常簡略的,只需獲得要存儲在甚麼處所的地址,再經由過程輸出輸入流,寫到這個地址中去就好了。絕年夜部門任務,struts2曾經幫我們做好了。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。