In general, there are two ways to upload images. One is to write the image file into the database, and the other is to store it in the server file directory. The image files written to the database need to be converted into a binary stream format, which occupies database space and is suitable for storage of a small number of images. For example, some small icons in the system have the advantage of writing to the database that they are relatively safe and are not easily avoided by users. Be careful to delete.
Implemented in struts2 (taking image upload as an example)
1.The FileUpload.jsp code list is as follows:
<%@ 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" name space="/">< s:file name="myFile" label="MyFile" ></s:file><s:textfield name="caption" label="Caption" ></s:textfield><s:submit label="Submit" >/s:submit></s:form></body></html>
2. The function list of ShowUpload.jsp is as follows:
<%@ 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 ="UploadIma ges/<s: property value ="imageFileName"/> "/><br /><s:property value ="caption"/></div >/body></html>
3. The code list of FileUploadAction.java is as follows:
package com.chris;import java.io.*;import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Active onSupport;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.//So, myFileContentType, myFileFi must be provided at the same time leName's 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, bound to page attributes pub lic 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;}public void setMyFile(File myFile) {this .myFile = myFile;}public String getImageFileName() {ret imageFileName;}public String getCaption() {ret urn 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), BU FFER_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;}}Note: At this time, only for the convenience of implementing Action, the ActionSupport is inherited and the Overrider execute() method is
Any POJO in struts2 can be used as an Action
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://strut s.apache.org/ dtds/struts-2.0.dtd"><struts><package name="example" namespace="/" extends="struts-default"><action name="fileUpload"><interceptor -ref name="fileUploadStack"/ ><result>/ShowUpload.jsp</result></action></package></struts>
5. The 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-pattern > </filter-mapping > <filter> <filter-name>struts2</fi lter -name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping><filter-name>struts2</filter-na me> <url-pattern>/ *</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>Index.jsp</welcome-file> </welcome-file-list></web-app>
The above content is all the content of how to upload images in Java struts2 introduced to you. I hope you like it.