欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Struts图片上传至指定文件夹并显示图片功能

程序员文章站 2024-02-21 13:44:40
继上一次利用servlet实现图片上传,这次利用基于mvc的struts框架,封装了servlet并简化了jsp页面跳转。 jsp上传页面 上传一定要为form加上en...

继上一次利用servlet实现图片上传,这次利用基于mvc的struts框架,封装了servlet并简化了jsp页面跳转。

jsp上传页面

上传一定要为form加上enctype="multipart/form-data",表示提交的数据时二进制的

并且必须是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>

struts.xml配置(maven项目放在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> 

teststruts.java控制类

一定要提供三个属性

file img;
string imgfilename;
string imgcontenttype;

然后为这3个属性提供getter setter方法

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";
  }
}

tomcat中server.xml文件配置虚拟路径

<context docbase="d:/imagebystruts" path="/imagebystruts" reloadable="true"/>
<context docbase="sshtest" path="/sshtest" reloadable="true" source="org.eclipse.jst.j2ee.server:sshtest"/></host>

success.jsp显示图片

<%@ 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>

总结

以上所述是小编给大家介绍的java struts图片上传至指定文件夹并显示图片功能,希望对大家有所帮助