2 qq 20380415 qq_20380415 于 2016.02.19 10:46 提问

跪求各位大神帮助啊,Struts2文件下载GG,有中文字符的文件下载就出错,英文就不出错,求救

本人小白,文件上传木有问题,下载英文字符文件名的文件也木有问题,就是中文字符名称的文件就GG了,跪求各位大神指点

9个回答

qq_20380415
qq_20380415   2016.02.19 10:47

private static final long serialVersionUID = 1L;
//文件路径
private String filePath;
//文件名称
private String fileName;

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

/** 得到文件输入流 */
public InputStream getSelectFile() throws Exception {
    return new FileInputStream(new File(filePath));
}

/** 将文件名转换成中文 */
public String getDownloadName() throws Exception {
    String downloadName = new String(fileName.getBytes(), "ISO8859-1");
    return downloadName;
}

@Override
public String execute() throws Exception {
    return SUCCESS;
}
    代码也是参照网上大神的来的,自己不是太明白,所以出错了也弄不过来,怪字迹太笨了,没学习好
qq_20380415
qq_20380415   2016.02.19 10:52

上传和下载英文字符文件名的文件都是没问题的![图片说明](https://img-ask.csdn.net/upload/201602/19/1455850343_708969.png)图片说明

qq_20380415
qq_20380415   2016.02.19 10:52

图片说明

qq_20380415
qq_20380415   2016.02.19 10:54

可是一到中文字符文件名的文件,也就是图片上的筷子扣那个文件,就GG了
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [selectFile] in the invocation stack. Check the tag specified for this action.
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
root cause

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [selectFile] in the invocation stack. Check the tag specified for this action.
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:189)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)

qq_20380415
qq_20380415   2016.02.19 10:55

配置文件也是OK 的

    <action name="download" class="com.java.action.DownLoadAction">
        <result name="success" type="stream">
            <!-- 支持所有格式文件 -->
            <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
            <!-- 得到输入流 -->
            <param name="inputName">selectFile</param>
            <param name="contentDisposition">attachment;filename="${downloadName}"</param>
            <param name="bufferSize">4096</param>
        </result>
    </action>   
qq_20380415
qq_20380415   2016.02.19 10:55

因为是参考或者是复制网上大神的代码,也不知道哪里不同导致的问题
package com.java.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
//文件路径
private String filePath;
//文件名称
private String fileName;

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

/** 得到文件输入流 */
public InputStream getSelectFile() throws Exception {
    return new FileInputStream(new File(filePath));
}

/** 将文件名转换成中文 */
public String getDownloadName() throws Exception {
    String downloadName = new String(fileName.getBytes(), "ISO8859-1");
    return downloadName;
}

@Override
public String execute() throws Exception {
    return SUCCESS;
}

}

qq_20380415
qq_20380415   2016.02.19 10:56

跪求大神们带飞啊,求指点,小鸟我实在玩不动了

lftaoyuan
lftaoyuan   2016.02.19 11:16

你看看是不是字符集格式的问题,GBK,UTF-8都试一下

qq_20380415
qq_20380415 好的
2 年多之前 回复
a510835147
a510835147   2016.02.19 14:27

估计你发送这个get请求到服务器的时候,就已经乱码了。 你可以输出下fileName参数。看看是不是已经乱码了

qq_20380415
qq_20380415 好的,谢谢您的建议
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
struts2文件下载错误提示的解决方法
1.   jsp页面超链接${name}2.   表单提交
使用struts2实现下载功能遇到的文件中文问题解决经验分享
今天在做一个项目,使用了struts2实现了一个下载的功能。 当在页面点击下载之后,就报错了。 一开始我就怀疑是不是文件名中文的问题,于是乎,我把文件名改成了英文的。 Bingo!果然可以正常下载了。那么说我的怀疑没有事正确的,然后开始想办法解决问题了。 先从我的下载的链接的传值那里开始入手,我的下载链接是这样的:<a href="download.action?name=通信原理(第6版)
struts2文件下载极其简单struts2文件下载极其简单struts2文件下载极其简单struts2文件下载极其简单
struts2文件下载极其简单struts2文件下载极其简单struts2文件下载极其简单struts2文件下载极其简单
Struts2文件下载时,中文文件名和chorm不兼容问题
以下内容为转载,如有冒犯,请见谅。 经过本人实验该方法非常简单: Struts2文件下载时,如果所下载的文件名是中文时,就会出现乱码。 解决办法如下: 步奏一:修改fileName属性的set方法 public void setFileName(String fileName) throws UnsupportedEncodingException { this.fileName =
struts2文件下载及文件名中文问题
1. struts2配置文件:<result name="testsuccess" type="stream"> <!-- 声明下载时存储文件流的变量名 --> <param name="inputName">testStream</param> <param name="contentType">application/octet-stream;charset=GBK</param> <!-- f
老生常谈:struct2文件下载(带点击取消出现的异常解决办法)
网上关于Struts2文件下载的文章太多了,但
struts2解决文件上传下载中文乱码问题
解决上传中文乱码问题 异常信息: "Can not find a Java.io.InputStream with the name [downLoadFile] in the invocation stack. Check the tag specified for this action." 解决方法 第一步:struts.xml中配置统一编码 第二步:在DownLoadFileA
Struts2实现下载文件
Struts2框架和文件下载有关的类是org.apache.struts2.dispatcher.StreamResult,这个类的一些成员变量和下载有关,看源码 //下载文件的类型 protected String contentType = "text/plain"; //下载文件的长度 protected String contentLength; //
Struts文件下载详解
在做项目中上传下载肯定是少不了的,本博文对struts2的下载进行解释并附上部分代码 1、action类 public class FileDownLoadAction extends ActionSupport{ private String filename;//下载文件的名称 @Override public String execute() throws Exception
win7旗舰版64位帮助和支持文件Help
win7旗舰版64位帮助和支持文件Help (修复C盘help文件夹下 文件丢失导致帮助错误的问题) 解压包分2卷---2卷都下载好 解压后的文件 复制到 系统盘/windows/help 目录下. 亲测