Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
總的說圖片上傳有兩種方式,一種是把圖片文件寫到數(shù)據(jù)庫(kù)中,另一種是存到服務(wù)器文件目錄中。寫到數(shù)據(jù)庫(kù)中的圖片文件需要轉(zhuǎn)換成二進(jìn)制流的格式,占用數(shù)據(jù)庫(kù)空間比較,適合少量圖片的存儲(chǔ),比如說,系統(tǒng)中某些小圖標(biāo),寫到數(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;//文件說明,與頁(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ù)器并把上傳的圖片讀取出來
- java web圖片上傳和文件上傳實(shí)例
- java通過模擬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)文章
熟練掌握J(rèn)ava8新特性之Stream API的全面應(yīng)用
Stream是Java8的一大亮點(diǎn),是對(duì)容器對(duì)象功能的增強(qiáng),它專注于對(duì)容器對(duì)象進(jìn)行各種非常便利、高效的 聚合操作(aggregate operation)或者大批量數(shù)據(jù)操作。Stream API借助于同樣新出現(xiàn)的Lambda表達(dá)式,極大的提高編程效率和程序可讀性,感興趣的朋友快來看看吧2021-11-11
Mybatis原始執(zhí)行方式Executor代碼實(shí)例
這篇文章主要介紹了Mybatis原始執(zhí)行方式Executor代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
學(xué)會(huì)Pulsar Consumer的使用方式
這篇文章主要介紹了Pulsar Consumer的使用方式,全文使用大量的代碼來做了詳細(xì)的講解,感興趣的小伙伴可以參考一下這篇文章,希望讀完能對(duì)你有很大的幫助2021-08-08
mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析
這篇文章主要為大家介紹了mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Win10 Java jdk14.0.2安裝及環(huán)境變量配置詳細(xì)教程
這篇文章主要介紹了Win10 Java jdk14.0.2安裝及環(huán)境變量配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

