• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java中关于Struts图片上传到指定文件夹下并显示图片功能的实例

java 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

这篇文章主要介绍了Java Struts图片上传至指定文件夹并显示图片功能,需要的朋友可以参考下

继上一次利用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(Stri<span style="color:transparent">本文来源gaodai#ma#com搞*!代#%^码网%</span>ng 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图片上传到指定文件夹下并显示图片功能的实例的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java中关于Struts图片上传到指定文件夹下并显示图片功能的实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址