Java框架Struts2實現(xiàn)圖片上傳功能
Struts 2 框架為處理文件上傳提供了內(nèi)置支持,它使用“在 HTML 中基于表單的文件上傳”。當上傳一個文件時,它通常會被存儲在一個臨時目錄中,而且它們應該由 Action 類進行處理或移動到一個永久的目錄,用來確保數(shù)據(jù)不丟失。服務器在恰當?shù)奈恢每赡苡幸粋€安全策略,它會禁止你寫到除了臨時目錄以外的目錄,而且這個目錄屬于你的web應用應用程序。
通過預定義的名為文件上傳的攔截器,Struts 的文件上傳是可能的,這個攔截器在 org.apache.struts2.interceptor.FileUploadInterceptor 類是可用的,而且是 defaultStack 的一部分。
創(chuàng)建視圖文件
讓我們開始創(chuàng)建需要瀏覽和上傳選定的文件的視圖。因此,讓我們創(chuàng)建一個帶有簡單的 HTML 上傳表單的 index.jsp,它允許用戶上傳文件:(表單的編碼類型設置為multipart/form-data)
<%--
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頁面:
<%-- 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頁面
<%-- 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)建一個稱為 uploadFile.java 的 Java 類,它負責上傳文件,并且把這個文件存儲在一個安全的位置:
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;
}
}
配置文件
<?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>
界面截圖


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片
這篇文章主要為大家詳細介紹了利用openoffice+jodconverter-code-3.0-bate4實現(xiàn)ppt轉(zhuǎn)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
LambdaQueryWrapper與QueryWrapper的使用方式
這篇文章主要介紹了LambdaQueryWrapper與QueryWrapper的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Java的Struts框架中<results>標簽的使用方法
這篇文章主要介紹了Java的Struts框架中<results>標簽的使用方法,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-11-11
Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例
這篇文章主要介紹了Java的PriorityBlockingQueue優(yōu)先級阻塞隊列代碼實例,PriorityBlockingQueue顧名思義是帶有優(yōu)先級的阻塞隊列,為了實現(xiàn)按優(yōu)先級彈出數(shù)據(jù),存入其中的對象必須實現(xiàn)comparable接口自定義排序方法,需要的朋友可以參考下2023-12-12
java?ThreadPoolExecutor線程池內(nèi)部處理流程解析
這篇文章主要為大家介紹了java?ThreadPoolExecutor線程池內(nèi)部處理流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Kafka 網(wǎng)絡中斷和網(wǎng)絡分區(qū)4種場景分析
這篇文章主要介紹了Kafka 網(wǎng)絡中斷和網(wǎng)絡分區(qū)4種場景分析2007-02-02

