怎么在Java项目中利用Struts将图片上传到指定文件夹?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。JSP上传页面上传一定要为fORM加上enctype=&
怎么在Java项目中利用Struts将图片上传到指定文件夹?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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><h2>成功</h2> <body> <img src="/imagebystruts/${imgFileName}"> </body> </body></html>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网精选频道,感谢您对编程网的支持。
--结束END--
本文标题: 怎么在Java项目中利用Struts将图片上传到指定文件夹
本文链接: https://www.lsjlt.com/news/224283.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
2024-05-01
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0