您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在Java中使用Struts2上傳圖片,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創建視圖文件
讓我們開始創建需要瀏覽和上傳選定的文件的視圖。因此,讓我們創建一個帶有簡單的 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>
之后創建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>
創建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>
創建 action 類
接下來讓我們創建一個稱為 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>
Java是一門面向對象編程語言,可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序。
上述就是小編為大家分享的怎么在Java中使用Struts2上傳圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。