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

今天主要分享开源框架Struts2文件上传实例过程,并且笔者将带着大家解决出现的一系列乱码问题,本文章中的重要部分将用特殊颜色标识,斜体表示不确定内容。笔者建议读者先快速阅读一遍本文,下载应具备的工具,再动手操作。或许写一遍比看十遍的功效更为明显。

笔者的Struts2版本号是2.2.3,如果你的是2.0版本以上也没关系。创建的java project项目名字为:uploads ,各个文件、页面编码统一为:UTF-8。

首 先导入Struts所依赖或自身的jar包,我们直接导入struts官方给出的空白实例所用到的jar包,分为:asm-3.1.jar、asm- commons-3.1.jar、asm-tree-3.1.jar、commons-fileupload-1.2.2.jar、commons- io-2.0.1.jar、commons-lang-2.5.jar、freemarker-2.3.16.jar、javassist- 3.11.0.GA.jar、ognl-3.0.1.jar、struts2-core-2.2.3.jar、xwork-core- 2.2.3.jar,这些包位于\struts-2.2.3\apps\struts2-blank\WEB-INF\lib目录下,如果你和笔者所使用 的struts版本不一致,请直接导入该目录下的所有文件。

uploads\WebRoot\WEB-INF\web.xml


    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">
    
        index.jsp
    

    
        uploads
        
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        

    

    
        uploads
        /*
    

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


    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">


    
    
                     class="com.lingdus.action.FileUploadAction">
            /index.jsp
        

    

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 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 + "/";
%>



    
        
        文件上传
        
        
        
        
        
        
    
    
        enctype="multipart/form-data" method="post" >
            
            
        

        上传结果
        



        ${result }
    

演示效果

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

你可能感兴趣的:(Struts2文件上传完美解决中文乱码问题)