Struts2實現(xiàn)上傳單個文件功能
upload.jsp 這個頁面選擇提交文件,提交到uploadImage.action
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="uploadImage.action" enctype="multipart/form-data" method="post"> please select the file:<input type="file" name="upload"> <input type="submit" value="上傳文件"> </form> </body> </html>
FileUploadAction.java 將傳來的file進行處理
package action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport{ private File upload; private String uploadFileName; private String uploadContentType; public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String execute(){ System.out.println(upload); System.out.println(uploadContentType); System.out.println(uploadFileName); String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName); System.out.println(savePath); try{ FileInputStream fis = new FileInputStream(upload); FileOutputStream fos = new FileOutputStream(savePath); IOUtils.copy(fis, fos); fos.flush(); fos.close(); fis.close(); }catch(Exception e){ e.printStackTrace(); } return "success"; } }
uploadFileName和uploadContentType,這兩個屬性分別用于封裝上傳文件的文件名、上傳文件的文件類型
Struts.xml配置
<action name="uploadImage" class="action.FileUploadAction"> <result name="success">uploadSuccess.jsp</result> <result name="input">uploadError.jsp</result> </action>
成功失敗界面隨便寫一個就行了,不貼了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
servlet監(jiān)聽實現(xiàn)統(tǒng)計在線人數(shù)功能 附源碼下載
這篇文章主要為大家詳細介紹了servlet監(jiān)聽統(tǒng)計在線人數(shù)的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot中yml的數(shù)據(jù)綁定示例
本文主要介紹了SpringBoot中yml的數(shù)據(jù)綁定示例,借助于YAML的簡潔語法和結(jié)構(gòu)化特性,我們能夠輕松地管理應(yīng)用程序的配置信息,使得配置文件更加清晰易讀,感興趣的可以了解一下2023-11-11DTO 實現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java中Final關(guān)鍵字的使用技巧及其性能優(yōu)勢詳解
這篇文章主要介紹了Java中Final關(guān)鍵字的使用技巧及其性能優(yōu)勢詳解,Java中的final關(guān)鍵字用于修飾變量、方法和類,可以讓它們在定義后不可更改,從而提高程序的穩(wěn)定性和可靠性,此外,final關(guān)鍵字還有一些使用技巧和性能優(yōu)勢,需要的朋友可以參考下2023-10-10Mybatis邏輯分頁與物理分頁PageHelper使用解析
這篇文章主要為大家介紹了Mybatis邏輯分頁與物理分頁PageHelper使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12