java Struts2框架下實(shí)現(xiàn)文件上傳功能
本文實(shí)例為大家分享了Struts2框架實(shí)現(xiàn)文件上傳的方法,供大家參考,具體內(nèi)容如下
struts2的配置過程
(1)在項(xiàng)目中加入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("/");
//這個(gè)獲取的path為:http://localhost:8080/sc
//上傳以后變?yōu)镋:\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);//標(biāo)題
System.out.println(uploadContentType);//準(zhǔn)備上傳的文件的文件類型
System.out.println(uploadFileName);//準(zhǔn)備上傳的文件的文件名,記住還有擴(kuò)展名
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];//定義一個(gè)1024大小的字節(jié)數(shù)組
int len=-1;//用來(lái)做標(biāo)志位
while((len=fis.read(bytes))>0){
fos.write(bytes, 0, len);
}
return null;
}
}
(5)JSP頁(yè)面的編寫
<%@ page contentType="text/html;charset=utf-8"%>
<!-- enctype="multipart/form-data",這個(gè)是最重要的配置 -->
<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>
經(jīng)過這次總結(jié),感覺struts2框架下的單文件的上傳還是非常簡(jiǎn)單的,只要獲取要存儲(chǔ)在什么地方的地址,再通過輸入輸出流,寫到這個(gè)地址中去就行了。絕大部分工作,struts2已經(jīng)幫我們做好了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- struts2框架的登錄制作圖文教程
- struts2框架入門
- java struts2框架簡(jiǎn)介
- 概述Java的struts2框架
- Java的Struts2框架中攔截器使用的實(shí)例教程
- 使用MyEclipse 開發(fā)struts2框架實(shí)現(xiàn)登錄功能(結(jié)構(gòu)教程)
- 詳解在Java的Struts2框架中配置Action的方法
- Java的Struts2框架配合Ext JS處理JSON數(shù)據(jù)的使用示例
- 詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式
- 搭建EXTJS和STRUTS2框架(ext和struts2簡(jiǎn)單實(shí)例)
- Struts2框架初學(xué)接觸
相關(guān)文章
Java使用建造者模式實(shí)現(xiàn)辦理手機(jī)套餐功能詳解
這篇文章主要介紹了Java使用建造者模式實(shí)現(xiàn)辦理手機(jī)套餐功能,較為詳細(xì)的描述了建造者模式的概念、原理并結(jié)合實(shí)例形式分析了Java使用建造者模式實(shí)現(xiàn)的辦理手機(jī)套餐功能具體步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05
Java連接數(shù)據(jù)庫(kù)oracle中文亂碼解決方案
這篇文章主要介紹了Java連接數(shù)據(jù)庫(kù)oracle中文亂碼解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
使用jpa的時(shí)候set實(shí)體類屬性自動(dòng)持久化的解決方案
這篇文章主要介紹了使用jpa的時(shí)候set實(shí)體類屬性自動(dòng)持久化的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot+Apache tika實(shí)現(xiàn)文檔內(nèi)容解析的示例詳解
Apache tika是Apache開源的一個(gè)文檔解析工具,本文主要為大家介紹了如何在springboot中引入tika的方式解析文檔,感興趣的小伙伴可以了解一下2023-07-07
解決使用@ResponseBody后返回500錯(cuò)誤的問題
這篇文章主要介紹了解決使用@ResponseBody后返回500錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-09-09
SpringSecurity如何實(shí)現(xiàn)配置單個(gè)HttpSecurity
這篇文章主要介紹了SpringSecurity如何實(shí)現(xiàn)配置單個(gè)HttpSecurity,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
IDEA斷點(diǎn)調(diào)試,斷點(diǎn)不起作用的解決
這篇文章主要介紹了IDEA斷點(diǎn)調(diào)試,斷點(diǎn)不起作用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

