首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Struts2文件下传完美解决中文乱码有关问题

2012-09-07 
Struts2文件上传完美解决中文乱码问题uploads\WebRoot\WEB-INF\web.xml?xml version1.0 encodingUTF

Struts2文件上传完美解决中文乱码问题

uploads\WebRoot\WEB-INF\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">
?? ?<welcome-file-list>
?? ??? ?<welcome-file>index.jsp</welcome-file>
?? ?</welcome-file-list>
?? ?<filter>
?? ??? ?<filter-name>uploads</filter-name>
?? ??? ?<filter-class>
?? ??? ??? ?org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
?? ??? ?</filter-class>
?? ?</filter>
?? ?<filter-mapping>
?? ??? ?<filter-name>uploads</filter-name>
?? ??? ?<url-pattern>/*</url-pattern>
?? ?</filter-mapping>
</web-app>

uploads\src\struts.xml(注意:如果你的版本和笔者的不一样可以展开struts2-core-2.2.3.jar,打开struts-default.xml文件,大概在24-26行DOCTYPE内容替换以下粗体)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
??? "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
??? "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
?? ?<constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 解决乱码!! -->
?? ?<package name="uploads" extends="struts-default">
?? ??? ?<action name="uploads"
?? ??? ??? ?type="redirect">/index.jsp</result>
?? ??? ?</action>
?? ?</package>
</struts>

uploads\src\com\lingdus\action\FileUploadAction.java

package com.lingdus.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

?? ?private File file; // 这个和以下的内容在struts中有特殊的要求,具体请翻阅相关资料。
?? ?private String fileFileName;


?? ?public synchronized File getFile() {
?? ??? ?return file;
?? ?}

?? ?public synchronized void setFile(File file) {
?? ??? ?this.file = file;
?? ?}

?? ?public synchronized String getFileFileName() {
?? ??? ?return fileFileName;
?? ?}

?? ?public synchronized void setFileFileName(String fileFileName) {
?? ??? ?this.fileFileName = fileFileName;
?? ?}

?? ?@Override
?? ?public String execute() throws Exception {
?? ??? ?InputStream is = null;
?? ??? ?OutputStream os = null;
?? ??? ?try {
?? ??? ??? ?is = new FileInputStream(file);
?? ??? ??? ?os = new FileOutputStream("C:\" + fileFileName);
?? ??? ??? ?byte[] buffer = new byte[2875623];
?? ??? ??? ?int length = 0;
?? ??? ??? ?while (-1 != (length = is.read(buffer))) {
?? ??? ??? ??? ?os.write(buffer, 0, length);
?? ??? ??? ?}
?? ??? ??? ?Map<String, Object> session = ActionContext.getContext()
?? ??? ??? ??? ??? ?.getSession();
?? ??? ??? ?session.put("result", "文件成功上传至:" + "C:\" + fileFileName);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?return "ERROR";
?? ??? ?} finally {
?? ??? ??? ?os.close();
?? ??? ??? ?is.close();
?? ??? ?}
?? ??? ?return SUCCESS;
?? ?}
}

uploads\WebRoot\index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
?? ?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>
?? ??? ?<s:form action="uploads" enctype="multipart/form-data" method="post">
?? ??? ??? ?<s:file name="file" label="文件上传"></s:file>
?? ??? ??? ?<s:submit value="立即上传"></s:submit>
?? ??? ?</s:form>
?? ??? ?上传结果
?? ??? ?<hr />
?? ??? ?${result }
?? ?</body>
</html>

演示效果

Struts2文件下传完美解决中文乱码有关问题

Struts2文件下传完美解决中文乱码有关问题

本文转自北大青鸟成都锦江校区,原文链接 http://www.scbdqn.com/course/netjava/3269.html

热点排行