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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java Struts2框架下實現文件上傳功能

java Struts2框架下實現文件上傳功能

2020-06-26 15:19昵稱不好起啊 JAVA教程

這篇文章主要為大家詳細介紹了java Struts2框架下實現文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Struts2框架實現文件上傳的方法,供大家參考,具體內容如下

struts2的配置過程

(1)在項目中加入jar包

java Struts2框架下實現文件上傳功能

(2)web.xml中filter(過濾器)的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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配置文件的編寫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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類的編寫

?
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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頁面的編寫

?
1
2
3
4
5
6
7
8
<%@ page contentType="text/html;charset=utf-8"%>
 
<!-- enctype="multipart/form-data",這個是最重要的配置 -->
<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>
  

經過這次總結,感覺struts2框架下的單文件的上傳還是非常簡單的,只要獲取要存儲在什么地方的地址,再通過輸入輸出流,寫到這個地址中去就行了。絕大部分工作,struts2已經幫我們做好了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩一卡2卡3卡新区网站 | 亚洲国产婷婷俺也色综合 | 99国内精品久久久久久久黑人 | 亚洲狼人香蕉香蕉在线28 | 波多野结衣久久国产精品 | 和日本免费不卡在线v | 亚洲黄色天堂 | lilisha李丽莎喷水大胆在线 | 深夜成人| 国偷盗摄自产福利一区在线 | 麻豆网页 | 国产精品最新资源网 | 亚洲 欧美 国产 综合久久 | 美女脱了内裤打开腿让人羞羞软件 | 国产精品免费视频能看 | 久久久久激情免费观看 | 男女性潮高片无遮挡禁18 | 国产精品久久久久久久人人看 | 2048论坛永久入口 原创合集 | 和岳m的小说 | 成人观看免费大片在线观看 | 精品久久久久久综合网 | 天天射夜夜爽 | 男同互操| 亚洲精品二三区伊人久久 | 免费一级特黄特色大片在线 | 亚洲日本中文字幕在线2022 | 国产美女久久精品香蕉69 | 亚洲国产精品日本无码网站 | 禁忌高h | 99精品国产高清一区二区三区香蕉 | 日产乱码卡一卡2卡三卡四福利 | 日韩在线二区全免费 | 国产精品久久毛片蜜月 | 亚洲高清一区二区三区四区 | 久久久无码精品亚洲A片软件 | 2021海角社区最新版 | 欧美jjvideo| 国产色图片 | 日韩视频在线免费 | 东方影视欧美天天影院 |