日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java struts2上传文件_java Struts2框架下实现文件上传功能

發布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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框架下实现文件上传功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。