oraclejava8 2010-06-04 21:29
浏览 293
已采纳

struts2文件下载乱码

[b]一下是我页面下载链接和java类及配置文件,英文文件名迅雷和windows下载均正常,中文时迅雷得到的文件名是乱码,而且下载不了,用Windows右击目标另存为,报错63485 ERROR 2010-06-04 21:18:08304
org.apache.struts2.dispatcher.StreamResult Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the tag specified for this action,然后点击取消报错,[/b]

${dis.fileName}

package com.office.struts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.struts2.ServletActionContext;

import com.juyan.struts2.httpservlet.SrrFactory;
import com.office.dao.FileTreeDao;
import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {

/**
 * 文件下载类
 */
private static final long serialVersionUID = 1L;

FileTreeDao fileTreeDao;

String pt;

public String getContentType() {
    return contentType;
}

public void setContentType(String contentType) {
    this.contentType = contentType;
}

public String getContentDisposition() {
    return contentDisposition;
}

public void setContentDisposition(String contentDisposition) {
    this.contentDisposition = contentDisposition;
}

private String contentType;
private String contentDisposition;

public FileTreeDao getFileTreeDao() {
    return fileTreeDao;
}

public void setFileTreeDao(FileTreeDao fileTreeDao) {
    this.fileTreeDao = fileTreeDao;
}

String fileName;

public String getFileName() throws UnsupportedEncodingException {
     fileName=new String(fileName.getBytes(),"ISO-8859-1");  
    return fileName;
}

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

public InputStream getInputStream() {


    File file = new File(pt);
    try {
        InputStream inputStream=new FileInputStream(file);
        return inputStream;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }

}

public String execute() {
    System.out.println(SrrFactory.getHttpServletRequestObject().getParameter(
                    "fileName"));
    System.out.println(SrrFactory.getHttpServletRequestObject()
            .getParameter("filePath")
            + "\\"
            + SrrFactory.getHttpServletRequestObject().getParameter(
                    "fileName"));
    this.pt = SrrFactory.getHttpServletRequestObject().getParameter(
            "filePath")
            + "\\"
            + SrrFactory.getHttpServletRequestObject().getParameter(
                    "fileName");
    return "success";
}

}

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




application/octet-stream;charset=ISO8859-1
inputStream
attachment;filename="${fileName}"

4096



  • 写回答

1条回答 默认 最新

  • myali88 2010-06-07 13:52
    关注

    设置断点跟踪一下,看乱码什么时候出现的?我之前也是这样做下载,不过是把文件显示名称和文件实际名称分开的,所以没有中文问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错