struts2图片上传并且显示(注意乱码)

struts2图片上传并且显示(注意乱码)
需要的jar包
web.xml:

    <?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>  

上传的页面 index.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>  

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> 

上传成功的页面 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>  

注意!上传中文名称会出现问题:
需要在tomcat的server.xml中加入URIEncoding=”utf-8” (网页的编码是utf-8)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值