Java Struts圖片上傳至指定文件夾并顯示圖片功能
繼上一次利用Servlet實現(xiàn)圖片上傳,這次利用基于MVC的Struts框架,封裝了Servlet并簡化了JSP頁面跳轉(zhuǎn)。
JSP上傳頁面
上傳一定要為form加上enctype="multipart/form-data",表示提交的數(shù)據(jù)時二進制的
并且必須是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"/> 設(shè)置文件上傳最大值
<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>
總結(jié)
以上所述是小編給大家介紹的Java Struts圖片上傳至指定文件夾并顯示圖片功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法
這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法的相關(guān)資料,在開發(fā)過程中經(jīng)常會用到String 這個類進行操作,需要的朋友可以參考下2017-08-08
Spring Cloud Gateway實現(xiàn)灰度發(fā)布方案
灰度發(fā)布是在微服務(wù)中的表現(xiàn)為同一服務(wù)同時上線不同版本,讓一部分用戶使用新版本來驗證新特性,如果驗證沒有問題,則將所有用戶都遷移到新版本上,本文就來介紹一下如何實現(xiàn),感興趣的可以了解一下2023-12-12
java Iterator接口和LIstIterator接口分析
這篇文章主要介紹了java Iterator接口和LIstIterator接口分析的相關(guān)資料,需要的朋友可以參考下2017-05-05
mybatis批量update時報錯multi-statement not allow的問題
這篇文章主要介紹了mybatis批量update時報錯multi-statement not allow的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

