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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

基于Struts2的文件上传

發布時間:2024/9/27 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Struts2的文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Struts2本身并沒提供上傳的組件,我們可以通過調用上傳框架來實現文件的上傳。

一、配置上傳解析器

首先要配置項目的框架,也就是倒導入"struts2-core-2.2.1.jar"庫文件,找到org.apache.struts2包下的default.porperties資源文件。如下圖;資源文件中給出了不同的strus2的默認配置,我們可看到struts2默認是jakarta作為其文件上傳的解析器。


?jakarta是Commo-FileUpload的框架。如果要使用Commo-FileUpload框架來上傳文件,只需將"commons-fileupload-1.2.1.jar"和"commons-io-1.3.2.jar"兩個jar復制到項目中的WEB-INF/lib目錄下就可。

如果想要使用COS框架來上傳文件,只需將“cos.jar”復制到項目中就可以,然后在修改struts.multipart.parser常量值。

修改常量值有兩種方法,一是在"struts.xml"中修改,代碼如下:

<constant name="struts.multipart.paeser" value="cos"></constant>

二是在struts.properties中修改,代碼如下:

? sruts.multipart.parser=cos

?

二、實現文件上傳的Action

? 創建表單:upload.jsp

?? ? ? <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

Jsp代碼 ?
  • <% ??
  • String?path?=?request.getContextPath(); ??
  • String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ??
  • %> ??
  • ??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"> ??
  • <html> ??
  • ??<head> ??
  • ????<base?href="<%=basePath%>"> ??
  • ???? ??
  • ????<title>Struts2文件上傳</title> ??
  • ???? ??
  • ????<meta?http-equiv="pragma"?content="no-cache"> ??
  • ????<meta?http-equiv="cache-control"?content="no-cache"> ??
  • ????<meta?http-equiv="expires"?content="0">???? ??
  • ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3"> ??
  • ????<meta?http-equiv="description"?content="This?is?my?page"> ??
  • ????<!-- ??
  • ????<link?rel="stylesheet"?type="text/css"?href="styles.css"> ??
  • ????--> ??
  • ??
  • ??</head> ??
  • ?? ??
  • ??<body> ??
  • ???<center> ??
  • ????<h1>Struts?2完成上傳</h1> ??
  • ??????<form?action="upload.action"?method="post"?enctype="multipart/form-data"> ??
  • ????????<table> ??
  • ????????????<tr> ??
  • ????????????????<td>用戶名:</td> ??
  • ????????????????<td><input?type="text"?name="username"?></td> ??
  • ????????????</tr> ??
  • ????????????<tr> ??
  • ????????????????<td>上傳文件:</td> ??
  • ????????????????<td><input?type="file"?name="myFile"></td> ??
  • ????????????</tr> ??
  • ????????????<tr> ??
  • ????????????????<td><input?type="submit"?value="上傳"></td> ??
  • ????????????????<td><input?type="reset"></td> ??
  • ????????????</tr> ??
  • ????????</table> ??
  • ??????</form> ??
  • ??</center> ??
  • ??</body> ??
  • </html>??
  • <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>Struts2文件上傳</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><center><h1>Struts 2完成上傳</h1><form action="upload.action" method="post" enctype="multipart/form-data"><table><tr><td>用戶名:</td><td><input type="text" name="username" ></td></tr><tr><td>上傳文件:</td><td><input type="file" name="myFile"></td></tr><tr><td><input type="submit" value="上傳"></td><td><input type="reset"></td></tr></table></form></center></body> </html>

    ? 完成上傳Action

    package net.hncu.struts2.action;

    Java代碼 ?
  • import?java.io.File; ??
  • import?java.io.FileInputStream; ??
  • import?java.io.FileOutputStream; ??
  • import?java.io.InputStream; ??
  • import?java.io.OutputStream; ??
  • ??
  • import?org.apache.struts2.ServletActionContext; ??
  • ??
  • import?com.opensymphony.xwork2.ActionSupport; ??
  • ??
  • public?class?UploadAction?extends?ActionSupport?{ ??
  • ????//?username屬性用來封裝用戶名 ??
  • ????private?String?username; ??
  • ???? ??
  • ????//?myFile屬性用來封裝上傳的文件 ??
  • ????private?File?myFile; ??
  • ???? ??
  • ????//?myFileContentType屬性用來封裝上傳文件的類型 ??
  • ????private?String?myFileContentType; ??
  • ??
  • ????//?myFileFileName屬性用來封裝上傳文件的文件名 ??
  • ????private?String?myFileFileName; ??
  • ??
  • ???? ??
  • ????//獲得username值 ??
  • ????public?String?getUsername()?{ ??
  • ????????return?username; ??
  • ????} ??
  • ??
  • ????//設置username值 ??
  • ????public?void?setUsername(String?username)?{ ??
  • ????????this.username?=?username; ??
  • ????} ??
  • ??
  • ????//獲得myFile值 ??
  • ????public?File?getMyFile()?{ ??
  • ????????return?myFile; ??
  • ????} ??
  • ??
  • ????//設置myFile值 ??
  • ????public?void?setMyFile(File?myFile)?{ ??
  • ????????this.myFile?=?myFile; ??
  • ????} ??
  • ??
  • ????//獲得myFileContentType值 ??
  • ????public?String?getMyFileContentType()?{ ??
  • ????????return?myFileContentType; ??
  • ????} ??
  • ??
  • ????//設置myFileContentType值 ??
  • ????public?void?setMyFileContentType(String?myFileContentType)?{ ??
  • ????????this.myFileContentType?=?myFileContentType; ??
  • ????} ??
  • ??
  • ????//獲得myFileFileName值 ??
  • ????public?String?getMyFileFileName()?{ ??
  • ????????return?myFileFileName; ??
  • ????} ??
  • ??
  • ????//設置myFileFileName值 ??
  • ????public?void?setMyFileFileName(String?myFileFileName)?{ ??
  • ????????this.myFileFileName?=?myFileFileName; ??
  • ????} ??
  • ??
  • ????public?String?execute()?throws?Exception?{ ??
  • ???????? ??
  • ????????//基于myFile創建一個文件輸入流 ??
  • ????????InputStream?is?=?new?FileInputStream(myFile); ??
  • ???????? ??
  • ????????//?設置上傳文件目錄 ??
  • ????????String?uploadPath?=?ServletActionContext.getServletContext() ??
  • ????????????????.getRealPath("/upload"); ??
  • ???????? ??
  • ????????//?設置目標文件 ??
  • ????????File?toFile?=?new?File(uploadPath,?this.getMyFileFileName()); ??
  • ???????? ??
  • ????????//?創建一個輸出流 ??
  • ????????OutputStream?os?=?new?FileOutputStream(toFile); ??
  • ??
  • ????????//設置緩存 ??
  • ????????byte[]?buffer?=?new?byte[1024]; ??
  • ??
  • ????????int?length?=?0; ??
  • ??
  • ????????//讀取myFile文件輸出到toFile文件中 ??
  • ????????while?((length?=?is.read(buffer))?>?0)?{ ??
  • ????????????os.write(buffer,?0,?length); ??
  • ????????} ??
  • ????????System.out.println("上傳用戶"+username); ??
  • ????????System.out.println("上傳文件名"+myFileFileName); ??
  • ????????System.out.println("上傳文件類型"+myFileContentType); ??
  • ????????//關閉輸入流 ??
  • ????????is.close(); ??
  • ???????? ??
  • ????????//關閉輸出流 ??
  • ????????os.close(); ??
  • ???????? ??
  • ????????return?SUCCESS; ??
  • ????} ??
  • ??
  • }??
  • import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {// username屬性用來封裝用戶名private String username;// myFile屬性用來封裝上傳的文件private File myFile;// myFileContentType屬性用來封裝上傳文件的類型private String myFileContentType;// myFileFileName屬性用來封裝上傳文件的文件名private String myFileFileName;//獲得username值public String getUsername() {return username;}//設置username值public void setUsername(String username) {this.username = username;}//獲得myFile值public File getMyFile() {return myFile;}//設置myFile值public void setMyFile(File myFile) {this.myFile = myFile;}//獲得myFileContentType值public String getMyFileContentType() {return myFileContentType;}//設置myFileContentType值public void setMyFileContentType(String myFileContentType) {this.myFileContentType = myFileContentType;}//獲得myFileFileName值public String getMyFileFileName() {return myFileFileName;}//設置myFileFileName值public void setMyFileFileName(String myFileFileName) {this.myFileFileName = myFileFileName;}public String execute() throws Exception {//基于myFile創建一個文件輸入流InputStream is = new FileInputStream(myFile);// 設置上傳文件目錄String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");// 設置目標文件File toFile = new File(uploadPath, this.getMyFileFileName());// 創建一個輸出流OutputStream os = new FileOutputStream(toFile);//設置緩存byte[] buffer = new byte[1024];int length = 0;//讀取myFile文件輸出到toFile文件中while ((length = is.read(buffer)) > 0) {os.write(buffer, 0, length);}System.out.println("上傳用戶"+username);System.out.println("上傳文件名"+myFileFileName);System.out.println("上傳文件類型"+myFileContentType);//關閉輸入流is.close();//關閉輸出流os.close();return SUCCESS;}}

    ? 配置上傳Action

    ?? <?xml version="1.0" encoding="UTF-8" ?>

    Java代碼 ?
  • <!DOCTYPE?struts?PUBLIC ??
  • ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
  • ????"http://struts.apache.org/dtds/struts-2.0.dtd"> ??
  • <struts> ??
  • ??
  • ????<package?name="struts2"?extends="struts-default"> ??
  • ????????<action?name="upload"?class="net.hncu.struts2.action.UploadAction"> ??
  • ????????????<result?name="success">/result.jsp</result> ??
  • ????????????<result?name="input">/upload.jsp</result> ??
  • ????????</action> ??
  • ????</package> ??
  • ??
  • ????<!--?Add?packages?here?--> ??
  • ??
  • </struts>??
  • <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><package name="struts2" extends="struts-default"><action name="upload" class="net.hncu.struts2.action.UploadAction"><result name="success">/result.jsp</result><result name="input">/upload.jsp</result></action></package><!-- Add packages here --></struts>

    ? 測試頁面:



    ?

    ?

    ?

    ?

    總結

    以上是生活随笔為你收集整理的基于Struts2的文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

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