Java to struts2 as an example of how to achieve image upload

  • 2020-04-01 04:24:47
  • OfStack

There are two ways to upload images in general, one is to write the image file to the database, and the other is to save it to the server file directory. Write to the image file in the database needs to be converted into the format of binary stream, occupying database space, suitable for a small number of images, for example, some small ICONS in the system, write to the advantage of the database is more secure, not easy to be accidentally deleted by the user.

Implemented in struts2 (image upload for example)

1. The code list of fileupload.jsp 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" namespace="/">
<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 ="UploadImages/<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.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: the file /> At the same time and the myFile myFileContentType, myFileFileName binding
//So at the same time to provide myFileContentType myFileFileName set method
private File myFile; //Upload a file
private String contentType;//Upload a file type 
private String fileName; //Upload a file The name 
private String imageFileName;
private String caption;//File description, bound to page properties
public void setMyFileContentType(String contentType) {
System.out.println(" The file type  : " + contentType);
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
System.out.println(" The file name  : " + 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);
}
@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: at this point, the ActionSupport is inherited and the Overrider execute() method is overridden for convenience only

Any POJO can be used as an Action in struts2

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>

5. The list of web.xml 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</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 this site to introduce you to Java struts2 how to upload all the content, I hope you like.


Related articles: