Java以struts2为例介绍如何实现图片上传

时间:2021-05-20

总的说图片上传有两种方式,一种是把图片文件写到数据库中,另一种是存到服务器文件目录中。写到数据库中的图片文件需要转换成二进制流的格式,占用数据库空间比较,适合少量图片的存储,比如说,系统中某些小图标,写到数据库中的优点是比较安全,不容易被用户不小心删除。

在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://e-file-list></web-app>

以上内容是小编给大家介绍的Java struts2中如何实现图片上传的全部内容,希望大家喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章