Home > Java > javaTutorial > body text

An example of uploading Struts pictures to a specified folder and displaying pictures in Java

黄舟
Release: 2017-08-10 13:35:59
Original
1090 people have browsed it

This article mainly introduces the function of Java Struts to upload pictures to a specified folder and display pictures. Friends who need it can refer to it

Following the last use of Servlet to upload pictures, this time MVC-based Struts is used The framework encapsulates Servlets and simplifies JSP page jumps.

JSP upload page

Upload must add enctype="multipart/form-data" to the form, indicating that the submitted data is binary The

and must be method="post"


<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<s:form action="login" method="post" enctype="multipart/form-data">
 <s:file name="img" label="头像" />
 <s:submit value="上传" />
</s:form>
<!-- <form action="login" method="post" enctype="multipart/form-data">
  头像:<input type="file" name="img"></input>
  <input type="submit" values="上传"></input>
</form> -->
</body>
</html>
Copy after login

struts.xml configuration (maven project is placed in resources)


<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
  "http://struts.apache.org/dtds/struts-2.3.dtd"> 
  <struts> 
     <constant name="struts.multipart.maxSize" value="20480000"/> 设置文件上传最大值
    <package name="struts2" extends="struts-default"> 
      <action name="login" class="com.controller.TestStruts" method="logintest"> 
        <result name="fail">/fail.jsp</result> 
        <result name="success">/success.jsp</result>
      </action> 
    </package> 
  </struts>
Copy after login

TestStruts.java control class

Be sure to provide three attributes


File img;
String imgFileName;
String imgContentType;
Copy after login

Then for this 3 properties provide getter setter methods


package com.controller;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TestStruts extends ActionSupport{
  private File img;
  private String imgFileName;
  private String imgContentType;
  public File getImg(){
    return img;
  }
  public String getimgFileName(){
    return imgFileName;
  }
  public String getImgContentType(){
    return imgContentType;
  }
  public void setImg(File img){
    this.img = img;
  }
  public void setImgFileName(String imgFileName){
    this.imgFileName = imgFileName;
  }
  public void setImgFileContentType(String imgContentType){
    this.imgContentType = imgContentType;
  }
  @SuppressWarnings("unchecked")
  public String logintest() throws IOException{
    Map p = ActionContext.getContext().getSession();
    p.put("imgFileName", imgFileName);
    File f = new File("D://imagebystruts");
    if (!f.exists()) { 
      f.mkdir(); 
    }     
    FileUtils.copyFile(img, new File(f, imgFileName));
    return "success";
  }
}
Copy after login

The server.xml file in Tomcat configures the virtual path


<Context docBase="D:/imagebystruts" path="/imagebystruts" reloadable="true"/>
<Context docBase="SSHTest" path="/SSHTest" reloadable="true" source="org.eclipse.jst.j2ee.server:SSHTest"/></Host>
Copy after login

success. jsp display picture


<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>success</title>
</head>
<body>
<h1>成功</h1>
 <body> 
  <img src="/imagebystruts/${imgFileName}">
 </body> 
</body>
</html>
Copy after login

Summary

The above is the detailed content of An example of uploading Struts pictures to a specified folder and displaying pictures in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template