Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
總的說(shuō)圖片上傳有兩種方式,一種是把圖片文件寫(xiě)到數(shù)據(jù)庫(kù)中,另一種是存到服務(wù)器文件目錄中。寫(xiě)到數(shù)據(jù)庫(kù)中的圖片文件需要轉(zhuǎn)換成二進(jìn)制流的格式,占用數(shù)據(jù)庫(kù)空間比較,適合少量圖片的存儲(chǔ),比如說(shuō),系統(tǒng)中某些小圖標(biāo),寫(xiě)到數(shù)據(jù)庫(kù)中的優(yōu)點(diǎn)是比較安全,不容易被用戶不小心刪除。
在struts2中實(shí)現(xiàn)(以圖片上傳為例)
1.FileUpload.jsp代碼清單如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>The FileUplaodDemo In Struts2</title> </head> <body> <s:form action="fileUpload" method="post" enctype="multipart/form-data" namespace="/"> <s:file name="myFile" label="MyFile" ></s:file> <s:textfield name="caption" label="Caption"></s:textfield> <s:submit label="提交"></s:submit> </s:form> </body> </html>
2.ShowUpload.jsp的功能清單如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>ShowUpload</title> </head> <body> <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" > <img src ="UploadImages/<s:property value ="imageFileName"/> "/> <br /> <s:property value ="caption"/> </div > </body> </html>
3.FileUploadAction.java的代碼清單如下 :
package com.chris;
import java.io.*;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;
//注意,文件上傳時(shí)<s:file/>同時(shí)與myFile,myFileContentType,myFileFileName綁定
//所以同時(shí)要提供myFileContentType,myFileFileName的set方法
private File myFile; //上傳文件
private String contentType;//上傳文件類型
private String fileName; //上傳文件名
private String imageFileName;
private String caption;//文件說(shuō)明,與頁(yè)面屬性綁定
public void setMyFileContentType(String contentType) {
System.out.println("文件類型 : " + contentType);
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
System.out.println("文件名稱 : " + fileName);
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}
注:此時(shí)僅為方便實(shí)現(xiàn)Action所以繼承ActionSupport,并Overrider execute()方法
在struts2中任何一個(gè)POJO都可以作為Action
4.struts.xml清單如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="example" namespace="/" extends="struts-default"> <action name="fileUpload" class="com.chris.FileUploadAction"> <interceptor-ref name="fileUploadStack"/> <result>/ShowUpload.jsp</result> </action> </package> </struts>
5.web.xml清單如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter > <filter-name > struts-cleanup </filter-name > <filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class > </filter > <filter-mapping > <filter-name > struts-cleanup </filter-name > <url-pattern > /* </url-pattern > </filter-mapping > <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>Index.jsp</welcome-file> </welcome-file-list> </web-app>
以上內(nèi)容是小編給大家介紹的Java struts2中如何實(shí)現(xiàn)圖片上傳的全部?jī)?nèi)容,希望大家喜歡。
- Java實(shí)現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來(lái)
- java web圖片上傳和文件上傳實(shí)例
- java通過(guò)模擬post方式提交表單實(shí)現(xiàn)圖片上傳功能實(shí)例
- JavaWeb實(shí)現(xiàn)裁剪圖片上傳完整代碼
- Java圖片上傳實(shí)現(xiàn)代碼
- java使用CKEditor實(shí)現(xiàn)圖片上傳功能
- Java實(shí)現(xiàn)的圖片上傳工具類完整實(shí)例
- 微信 java 實(shí)現(xiàn)js-sdk 圖片上傳下載完整流程
- Java+mysql本地圖片上傳數(shù)據(jù)庫(kù)及下載示例
- java實(shí)現(xiàn)多圖片上傳功能
相關(guān)文章
Windows系統(tǒng)下Eclipse搭建ESP32編譯環(huán)境及安裝過(guò)程
Ecppse 使用了 ESP-IDF 中的 Makefile 支持。這意味著您需要從創(chuàng)建 ESP-IDF 項(xiàng)目開(kāi)始。您可以使用 github 中的 idf-template 項(xiàng)目,接下來(lái)通過(guò)本文給大家介紹Windows系統(tǒng)下Eclipse搭建ESP32編譯環(huán)境及安裝過(guò)程,感興趣的朋友一起看看吧2021-10-10
在java中main函數(shù)如何調(diào)用外部非static方法
這篇文章主要介紹了在java中main函數(shù)如何調(diào)用外部非static方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布小游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單石頭剪刀布小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
將Java程序的輸出結(jié)果寫(xiě)入文件方法實(shí)例
這篇文章主要給大家介紹了關(guān)于將Java程序的輸出結(jié)果寫(xiě)入文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
一篇文章帶你了解spring事務(wù)失效的多種場(chǎng)景
在日常編碼過(guò)程中常常涉及到事務(wù),在前兩天看到一篇文章提到了Spring事務(wù),那么在此總結(jié)下在Spring環(huán)境下事務(wù)失效的幾種原因.2021-09-09
scala當(dāng)中的文件操作和網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)方法
這篇文章主要介紹了scala當(dāng)中的文件操作和網(wǎng)絡(luò)請(qǐng)求的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

