Java Struts圖片上傳至指定文件夾并顯示圖片功能
繼上一次利用Servlet實(shí)現(xiàn)圖片上傳,這次利用基于MVC的Struts框架,封裝了Servlet并簡(jiǎn)化了JSP頁(yè)面跳轉(zhuǎn)。
JSP上傳頁(yè)面
上傳一定要為form加上enctype="multipart/form-data",表示提交的數(shù)據(jù)時(shí)二進(jìn)制的
并且必須是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項(xiàng)目放在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控制類(lèi)
一定要提供三個(gè)屬性
File img; String imgFileName; String imgContentType;
然后為這3個(gè)屬性提供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圖片上傳至指定文件夾并顯示圖片功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
使用Spring Security集成手機(jī)驗(yàn)證碼登錄功能實(shí)現(xiàn)
本文詳細(xì)介紹了如何利用SpringSecurity來(lái)實(shí)現(xiàn)手機(jī)驗(yàn)證碼的注冊(cè)和登錄功能,在登錄過(guò)程中,同樣需通過(guò)驗(yàn)證碼進(jìn)行驗(yàn)證,文章還提供了相關(guān)的代碼實(shí)現(xiàn)2024-10-10
Java的Socket實(shí)現(xiàn)長(zhǎng)連接以及數(shù)據(jù)的發(fā)送和接收方式
這篇文章主要介紹了Java的Socket實(shí)現(xiàn)長(zhǎng)連接以及數(shù)據(jù)的發(fā)送和接收方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring實(shí)戰(zhàn)之緩存使用key操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用key操作,結(jié)合實(shí)例形式分析了Spring緩存使用key具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
eclipse啟動(dòng)tomcat無(wú)法訪(fǎng)問(wèn)的解決方法
這篇文章介紹了eclipse啟動(dòng)tomcat無(wú)法訪(fǎng)問(wèn)的解決方法,有需要的朋友可以參考一下2013-10-10
解決Java執(zhí)行Cmd命令出現(xiàn)的死鎖問(wèn)題
這篇文章主要介紹了關(guān)于Java執(zhí)行Cmd命令出現(xiàn)的死鎖問(wèn)題解決,解決方法就是在waitfor()方法之前讀出窗口的標(biāo)準(zhǔn)輸出、輸出、錯(cuò)誤緩沖區(qū)中的內(nèi)容,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
并發(fā)編程ConcurrentLinkedQueue示例詳解

