Java takes struts2 as an example to introduce how to implement picture upload _java

Source: Internet
Author: User

In general, there are two ways to upload pictures, one is to write the picture file to the database, the other is to save to the server file directory. Write to the database of the picture file needs to be converted into a binary stream format, occupy the database space comparison, suitable for a small number of pictures of storage, for example, the system of some small icons, write to the advantages of the database is relatively safe, not easy to be accidentally deleted by users.

Implement in Struts2 (take picture upload for example)

The 1.FILEUPLOAD.JSP code list is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%@ taglib prefix=
"s" uri= "/ Struts-tags "%>
 
 

2.showupload.jsp's list of functions is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%@ taglib prefix=
"s" uri= "/ Struts-tags "%>
 
 

3.fileuploadaction.java's code list is as follows:

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; Note that when the file is uploaded <s:file/> is bound to myfile,myfilecontenttype,myfilefilename at the same time//so provide myfilecontenttype, Myfilefilename Set method Private File myFile; Upload file private string contenttype;//upload file type private string fileName;
Upload file name private String imagefilename; Private String caption;//file description, binding to page Properties public void Setmyfilecontenttype (String contentType) {System.out.println ("file type:
"+ ContentType);
this. ContentType = ContentType;
public void Setmyfilefilename (String fileName) {System.out.println ("file name:" + filename);
this. filename = filename; 
The public void Setmyfile (File myFile) {this. MyFile = myFile} is public String getimagefilename () {return imagefilename;} Public String GetcaptIon () {return caption} is public void Setcaption (String caption) {this. Caption = caption;} private static void copy (Fil
e src, File dst) {try {inputstream in = null;
OutputStream out = null; try {in = new Bufferedinputstream (new FileInputStream (SRC), buffer_size); out = new Bufferedoutputstream (New Fileoutput
Stream (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; }
}

Note: Inherit actionsupport at this time only to facilitate implementation of action, and Overrider execute () method

Any pojo in the STRUTS2 can act as an action

The 4.struts.xml list is as follows:

<?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>

The 5.web.xml list is as follows:

<?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-patte RN > </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> 

The above content is small to introduce you to the Java struts2 How to achieve the full picture upload content, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.