java struts2上传文件_java Struts2框架下实现文件上传功能
本文實例為大家分享了struts2框架實現文件上傳的方法,供大家參考,具體內容如下
struts2的配置過程
(1)在項目中加入jar包
(2)web.xml中filter(過濾器)的配置
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">
struts2
org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter
struts2
/*
(3)struts.xml配置文件的編寫
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">
struts2
org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter
struts2
/*
(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頁面的編寫
文件名:
上傳:
經過這次總結,感覺struts2框架下的單文件的上傳還是非常簡單的,只要獲取要存儲在什么地方的地址,再通過輸入輸出流,寫到這個地址中去就行了。絕大部分工作,struts2已經幫我們做好了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網。
希望與廣大網友互動??
點此進行留言吧!
總結
以上是生活随笔為你收集整理的java struts2上传文件_java Struts2框架下实现文件上传功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vamei java_java Vame
- 下一篇: java string 前缀匹配_字符串