一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Struts2實現文件上傳功能實例解析

Struts2實現文件上傳功能實例解析

2020-07-24 12:22Giving_bestself Java教程

這篇文章主要介紹了Struts2實現文件上傳功能實例解析,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

一、 搭建struts2環境

在myeclipse下,右擊項目->MyEclipse->Project Facets->install Apache Struts2。

如要自己搭建,需下載struts2包,寫struts.xml配置文件。

web.xml文件配置如下:

?
1
2
3
4
5
6
7
8
<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>*.action</url-pattern>
 </filter-mapping>

二、 文件上傳

1.前臺頁面:

上傳頁面:

?
1
2
3
4
5
6
7
8
<body>
 <form action="upload.action" method="post" enctype="multipart/form-data">
  <input type="file" name="upload"/>
  <input type="submit" value="提交"/>
  <br>
  ${result}
 </form>
 </body>

input name屬性與后臺命名一致。

上傳失敗頁面:

?
1
2
3
4
<body>
 <h2>上傳失敗</h2>
 <s:fielderror></s:fielderror>
 </body>

需:

?
1
<%@ taglib uri="/struts-tags" prefix="s"%>

2.后臺Action

主要屬性upload,uploadContentType,uploadFileName。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.yf.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
 private File upload;
 private String uploadContentType;
 private String uploadFileName;
 private String result;
 public File getUoload() {
  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 getResult() {
  return result;
 }
 public void setResult(String result) {
  this.result = result;
 }
 @Override
 public String execute() throws Exception {
  String path = ServletActionContext.getServletContext().getRealPath("/images");
  File file = new File(path);
  if(!file.exists()){
   file.mkdir();
  }
  System.out.println(upload);
  FileUtils.copyFile(upload, new File(file,uploadFileName));
  result = "上傳成功";
  return SUCCESS;
 }
}

3.struts.xml文件配置

配置action及配置攔截器限制上傳文件的類型及大小。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
 <constant name="struts.devMode" value="true"/>
 <constant name="struts.multipart.saveDir" value="/tmp"/>
 <constant name="struts.custom.i18n.resources" value="app"></constant>
 <package name="default" namespace="/" extends="struts-default">
  <action name="upload" class="com.yf.action.UploadAction">
  <result>/index.jsp</result>
  <result name="input">/error.jsp</result>
  <!-- 配置攔截器限制上傳文件的類型及大小 -->
  <interceptor-ref name="fileUpload">
   <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpeg</param>
   <param name="maximumSize">2M</param>
  </interceptor-ref>
  <interceptor-ref name="defaultStack"></interceptor-ref>
  </action>
 </package>
</struts>

4.新建properties文件

文件上傳失敗信息顯示到前臺,處理顯示出錯信息。

Struts2實現文件上傳功能實例解析

文件內容如下:

?
1
2
struts.messages.error.file.too/large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u4E86\uFF01
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u7B26\uFF01

即添加:

Name:struts.messages.error.file.too/large

value : 上傳文件太大了!

Name : struts.messages.error.content.type.not.allowed

value: 上傳文件類型不符!

運行結果如下:

選擇jpg圖片,大小不超過2M,運行后

Struts2實現文件上傳功能實例解析

選擇非圖片文件:

Struts2實現文件上傳功能實例解析

如需批量上傳文件,將后臺upload,uploadContentType,uploadFileName均改為List,循環讀取上傳文件保存到硬盤,前臺加入input ,name屬性一致。

以上所述是小編給大家介紹的Struts2實現文件上傳功能實例解析,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

原文鏈接:http://blog.csdn.net/giving_bestself/article/details/54233172

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本亚欧乱色视频在线观看 | 色多多在线视频 | 私人影院在线免费观看 | 国产高清自拍 | 含羞草传媒网站免费进入欢迎 | 好吊操这里有精品 | 亚洲四虎 | 99久久伊人精品波多野结衣 | 双性np玩烂了np欲之国的太子 | 亚洲人和日本人hd | 久久久精品3d动漫一区二区三区 | 欧美日韩在线一区二区三区 | 日韩在线观看免费 | 国产大胆歌舞团网站 | 国产成人一级 | 亚洲欧美久久一区二区 | 青青久久久 | 调教女帝 | 久久亚洲电影www电影网 | 日韩欧美亚洲国产高清在线 | 色老板在线 | 精品成人在线 | 成人免费体验区福利云点播 | 免费看视频高清在线观看 | 把老师操了 | 亚洲第一区se | 91亚洲精品丁香在线观看 | 国产精品馆 | 摸逼小说 | 成3d漫二区三区四区 | 亚洲精品专区 | 2022国产麻豆剧果冻传媒入口 | 轻轻色在线视频中文字幕 | 3d动漫美女被吸乳羞羞有 | 91夜夜操 | 国语自产拍在线播放不卡 | 国产精品免费久久久久影院 | 毛片www| 韩国三级大全 | 大又大又黄又爽免费毛片 | 午夜办公室在线观看高清电影 |