您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Java中使用Struts2上傳圖片,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)建視圖文件
讓我們開始創(chuàng)建需要瀏覽和上傳選定的文件的視圖。因此,讓我們創(chuàng)建一個帶有簡單的 HTML 上傳表單的 index.jsp,它允許用戶上傳文件:(表單的編碼類型設(shè)置為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 類,它負責(zé)上傳文件,并且把這個文件存儲在一個安全的位置:
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是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
上述就是小編為大家分享的怎么在Java中使用Struts2上傳圖片了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。