1. 程式人生 > >struts2 圖片上傳到伺服器並顯示在頁面

struts2 圖片上傳到伺服器並顯示在頁面

1、新建一個WEB專案。

2、匯入struts2所用的jar。


3、新建或複製struts2的配置檔案(struts.xml)。

4、在web.xml檔案中配置struts2功能。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  	<filter-name>strut2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>strut2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5、檔案上傳的頁面。(indes.jsp)如下內容所示:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>  

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Struts2 Common File Upload</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
  	<h3>Struts2 檔案上傳到伺服器</h3>
  	<form action="fileUpload" method="post" enctype="multipart/form-data">
  		檔案:<input type="file" name="fileImage"/>
  		<input type="submit" value="上傳"/>
  	</form>
  	<s:fielderror/>
  </body>
</html>

6、填寫配置struts.xml檔案的action。
<!DOCTYPE struts PUBLIC 
"-//Aoacge Siftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 解決亂碼 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<package name="com.upload.imageupload"  extends="struts-default"> 
		<action name="fileUpload" class="com.upload.imageupload.ImageFileUpload" method="execute">
			<result name="success">upload.jsp</result>
			<result name="error">error.jsp</result>
			 <!-- 動態設定savePath的屬性值 -->
			 <!-- <param name="savePath">/images</param> -->
            <interceptor-ref name="fileUpload">
                <!-- 檔案過濾 -->
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
                <!-- 檔案大小, 以位元組為單位 -->
                <param name="maximumSize">1024*20</param>
            </interceptor-ref>
            <!-- 預設攔截器必須放在fileUpload之後,否則無效 -->
            <interceptor-ref name="defaultStack" />
            <result name="input">index.jsp</result>
		</action>
	</package>
</struts>
7、上傳成功的頁面。(upload.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>圖片上傳成功</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	圖片上傳成功!
  	<br></br>
  	<img src="${pageContext.request.contextPath}/<s:property value="'images/'+fileImageFileName"/>">
    <s:debug></s:debug>
  </body>
</html>

8、上傳失敗的頁面。(error.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>圖片上傳失敗</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   	圖片上傳失敗!
  </body>
</html>

9、上傳中文的圖片。

在tomcat的server.xml中加入URIEncoding="utf-8" (網頁的編碼是utf-8)
 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="utf-8" redirectPort="8443" />