国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java以struts2為例介紹如何實現圖片上傳

2019-11-26 14:51:41
字體:
來源:轉載
供稿:網友

總的說圖片上傳有兩種方式,一種是把圖片文件寫到數據庫中,另一種是存到服務器文件目錄中。寫到數據庫中的圖片文件需要轉換成二進制流的格式,占用數據庫空間比較,適合少量圖片的存儲,比如說,系統中某些小圖標,寫到數據庫中的優點是比較安全,不容易被用戶不小心刪除。

在struts2中實現(以圖片上傳為例)

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 ;//注意,文件上傳時<s:file/>同時與myFile,myFileContentType,myFileFileName綁定//所以同時要提供myFileContentType,myFileFileName的set方法private File myFile; //上傳文件private String contentType;//上傳文件類型private String fileName; //上傳文件名private String imageFileName;private String caption;//文件說明,與頁面屬性綁定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);}@Overridepublic String execute() {imageFileName = new Date().getTime() + getExtention(fileName);File imageFile = new File(ServletActionContext.getServletContext().getRealPath("UploadImages" ) + "/" + imageFileName);copy(myFile, imageFile);return SUCCESS;}}

注:此時僅為方便實現Action所以繼承ActionSupport,并Overrider execute()方法

在struts2中任何一個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>

以上內容是小編給大家介紹的Java struts2中如何實現圖片上傳的全部內容,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 翁源县| 赣州市| 浏阳市| 宣城市| 荆门市| 江门市| 水富县| 六枝特区| 邻水| 彰化市| 文水县| 渝中区| 博野县| 肥西县| 平原县| 常德市| 平谷区| 嵊州市| 海口市| 博爱县| 武汉市| 逊克县| 弋阳县| 哈尔滨市| 景谷| 龙海市| 抚州市| 囊谦县| 南汇区| 洪洞县| 巴林左旗| 青浦区| 南阳市| 东台市| 赣榆县| 武川县| 渝北区| 当雄县| 奈曼旗| 东辽县| 古丈县|