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

来源:互联网 发布:大数据的工资待遇 编辑:程序博客网 时间:2024/06/03 20:17

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 0
原创粉丝点击