Java以struts2为例介绍如何实现图片上传
总的说图片上传有两种方式,一种是把图片文件写到数据库中,另一种是存到服务器文件目录中。写到数据库中的图片文件需要转换成二进制流的格式,占用数据库空间比较,适合少量图片的存储,比如说,系统中某些小图标,写到数据库中的优点是比较安全,不容易被用户不小心删除。
在struts2中实现(以图片上传为例)
1.FileUpload.jsp代码清单如下:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%> <%@taglibprefix="s"uri="/struts-tags"%> <html> <head> <title>TheFileUplaodDemoInStruts2</title> </head> <body> <s:formaction="fileUpload"method="post"enctype="multipart/form-data"namespace="/"> <s:filename="myFile"label="MyFile"></s:file> <s:textfieldname="caption"label="Caption"></s:textfield> <s:submitlabel="提交"></s:submit> </s:form> </body> </html>
2.ShowUpload.jsp的功能清单如下:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%> <%@taglibprefix="s"uri="/struts-tags"%> <html> <head> <title>ShowUpload</title> </head> <body> <divstyle="padding:3px;border:solid1px#cccccc;text-align:center"> <imgsrc="UploadImages/<s:propertyvalue="imageFileName"/>"/> <br/> <s:propertyvalue="caption"/> </div> </body> </html>
3.FileUploadAction.java的代码清单如下:
packagecom.chris; importjava.io.*; importjava.util.Date; importorg.apache.struts2.ServletActionContext; importcom.opensymphony.xwork2.ActionSupport; publicclassFileUploadActionextendsActionSupport{ privatestaticfinallongserialVersionUID=572146812454l; privatestaticfinalintBUFFER_SIZE=16*1024; //注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定 //所以同时要提供myFileContentType,myFileFileName的set方法 privateFilemyFile;//上传文件 privateStringcontentType;//上传文件类型 privateStringfileName;//上传文件名 privateStringimageFileName; privateStringcaption;//文件说明,与页面属性绑定 publicvoidsetMyFileContentType(StringcontentType){ System.out.println("文件类型:"+contentType); this.contentType=contentType; } publicvoidsetMyFileFileName(StringfileName){ System.out.println("文件名称:"+fileName); this.fileName=fileName; } publicvoidsetMyFile(FilemyFile){ this.myFile=myFile; } publicStringgetImageFileName(){ returnimageFileName; } publicStringgetCaption(){ returncaption; } publicvoidsetCaption(Stringcaption){ this.caption=caption; } privatestaticvoidcopy(Filesrc,Filedst){ try{ InputStreamin=null; OutputStreamout=null; try{ in=newBufferedInputStream(newFileInputStream(src),BUFFER_SIZE); out=newBufferedOutputStream(newFileOutputStream(dst),BUFFER_SIZE); byte[]buffer=newbyte[BUFFER_SIZE]; while(in.read(buffer)>0){ out.write(buffer); } }finally{ if(null!=in){ in.close(); } if(null!=out){ out.close(); } } }catch(Exceptione){ e.printStackTrace(); } } privatestaticStringgetExtention(StringfileName){ intpos=fileName.lastIndexOf("."); returnfileName.substring(pos); } @Override publicStringexecute(){ imageFileName=newDate().getTime()+getExtention(fileName); FileimageFile=newFile(ServletActionContext.getServletContext().getRealPath("UploadImages")+"/"+imageFileName); copy(myFile,imageFile); returnSUCCESS; } }
注:此时仅为方便实现Action所以继承ActionSupport,并Overriderexecute()方法
在struts2中任何一个POJO都可以作为Action
4.struts.xml清单如下:
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstrutsPUBLIC "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <packagename="example"namespace="/"extends="struts-default"> <actionname="fileUpload"class="com.chris.FileUploadAction"> <interceptor-refname="fileUploadStack"/> <result>/ShowUpload.jsp</result> </action> </package> </struts>
5.web.xml清单如下:
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="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>
以上内容是小编给大家介绍的Javastruts2中如何实现图片上传的全部内容,希望大家喜欢。