激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - Java框架Struts2實(shí)現(xiàn)圖片上傳功能

Java框架Struts2實(shí)現(xiàn)圖片上傳功能

2021-05-28 13:42yzj_xiaoyue Java教程

這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

struts 2 框架為處理文件上傳提供了內(nèi)置支持,它使用“在 html 中基于表單的文件上傳”。當(dāng)上傳一個(gè)文件時(shí),它通常會(huì)被存儲(chǔ)在一個(gè)臨時(shí)目錄中,而且它們應(yīng)該由 action 類進(jìn)行處理或移動(dòng)到一個(gè)永久的目錄,用來確保數(shù)據(jù)不丟失。服務(wù)器在恰當(dāng)?shù)奈恢每赡苡幸粋€(gè)安全策略,它會(huì)禁止你寫到除了臨時(shí)目錄以外的目錄,而且這個(gè)目錄屬于你的web應(yīng)用應(yīng)用程序。

通過預(yù)定義的名為文件上傳的攔截器,struts 的文件上傳是可能的,這個(gè)攔截器在 org.apache.struts2.interceptor.fileuploadinterceptor 類是可用的,而且是 defaultstack 的一部分。

創(chuàng)建視圖文件

讓我們開始創(chuàng)建需要瀏覽和上傳選定的文件的視圖。因此,讓我們創(chuàng)建一個(gè)帶有簡(jiǎn)單的 html 上傳表單的 index.jsp,它允許用戶上傳文件:(表單的編碼類型設(shè)置為multipart/form-data)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%--
 created by intellij idea.
 user: yzjxiaoyue
 date: 2017/7/28
 time: 19:11
 to change this template use file | settings | file templates.
--%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
     pageencoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
  <title>file upload</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
  <label for="myfile">upload your file</label>
  <input type="file" name="myfile" id="myfile"/>
  <input type="submit" value="upload"/>
</form>
</body>
</html>

之后創(chuàng)建success.jsp頁面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
 created by intellij idea.
 user: yzjxiaoyue
 date: 2017/7/28
 time: 19:14
 to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title>file upload success</title>
</head>
<body>
you have successfully uploaded <s:property value="myfilefilename"/>
</body>
</html>

創(chuàng)建error.jsp頁面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%--
 created by intellij idea.
 user: yzjxiaoyue
 date: 2017/7/28
 time: 20:05
 to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html; charset=utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title>file upload error</title>
</head>
<body>
there has been an error in uploading the file.
</body>
</html>

創(chuàng)建 action 類

接下來讓我們創(chuàng)建一個(gè)稱為 uploadfile.java 的 java 類,它負(fù)責(zé)上傳文件,并且把這個(gè)文件存儲(chǔ)在一個(gè)安全的位置:

?
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
package com.action;
 
import com.opensymphony.xwork2.actionsupport;
import org.apache.commons.io.fileutils;
 
import java.io.file;
import java.io.ioexception;
public class uploadfile extends actionsupport{
  private file myfile;
 
  public file getmyfile() {
    return myfile;
  }
  public void setmyfile(file myfile) {
    this.myfile = myfile;
  }
 
  private string myfilecontenttype;
 
  private string myfilefilename;
 
  private string destpath;
 
  public string execute()
  {
     /* copy file to a safe location */
    destpath = "e:\\program files\\apache-tomcat-9.0.0\\apache-tomcat-9.0.0.m22\\work\\";
    try{
      system.out.println("src file name: " + myfile);
      system.out.println("dst file name: " + myfilefilename);
      file destfile = new file(destpath, myfilefilename);
      fileutils.copyfile(myfile, destfile);
    }catch(ioexception e){
      e.printstacktrace();
      return error;
    }
    return success;
  }
 
 
  public string getmyfilecontenttype() {
    return myfilecontenttype;
  }
  public void setmyfilecontenttype(string myfilecontenttype) {
    this.myfilecontenttype = myfilecontenttype;
  }
  public string getmyfilefilename() {
    return myfilefilename;
  }
  public void setmyfilefilename(string myfilefilename) {
    this.myfilefilename = myfilefilename;
  }
}

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
 
<!doctype struts public
    "-//apache software foundation//dtd struts configuration 2.3//en"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
  <constant name="struts.devmode" value="true"/>
  <constant name="struts.multipart.maxsize" value="10000000"/>
 
  <constant name="struts.multipart.savedir" value="/tmp"/>
  <constant name="struts.custom.i18n.resources" value="struts"></constant>
  <package name="default" namespace="/" extends="struts-default">
    <action name="upload" class="com.action.uploadfile">
      <!--<interceptor-ref name="basicstack"/>-->
      <interceptor-ref name="defaultstack"/>
      <interceptor-ref name="fileupload">
        <param name="allowedtypes">image/jpeg,image/jpg,image/gif</param>
      </interceptor-ref>
      <result name="success">/success.jsp</result>
      <result name="error">/error.jsp</result>
    </action>
  </package>
</struts>

界面截圖

Java框架Struts2實(shí)現(xiàn)圖片上傳功能

Java框架Struts2實(shí)現(xiàn)圖片上傳功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/yzj_xiaoyue/article/details/76284279

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 88xx成人精品视频 | 91九色论坛 | 日本精品黄色 | 免费观看一区 | 国产网站黄 | 一级电影在线免费观看 | 特级西西444www大精品视频免费看 | 国产亚洲精品成人a | 中文字幕精品在线播放 | 粉嫩蜜桃麻豆免费大片 | 蜜桃网站在线 | 精品国产91久久久久久久 | 操网| 日韩视频一区二区在线观看 | 久久免费观看一级毛片 | 欧美 中文字幕 | 欧美天堂一区 | 极品美女一级毛片 | 99精品在线视频观看 | 欧美一级美国一级 | av成人免费在线观看 | 中文在线免费观看 | 亚洲 91 | 久久精品视频在线 | 久久欧美亚洲另类专区91大神 | 久久免费精品视频 | 久久综合综合久久 | 亚洲国产精品久久久久久久久久久 | 一本色道久久综合狠狠躁篇适合什么人看 | 欧美一区二区三区成人 | 国产毛片毛片毛片 | 欧美一级电影网 | 姑娘第四集免费看视频 | 久久国产夫妻视频 | 黄色试看视频 | 精品欧美一区二区精品久久 | 久久99亚洲精品久久99果 | 国产精品久久久久久久久久iiiii | av中文一区 | 九九热久久免费视频 | 少妇的肉体k8经典 |