ClassCastException错误 想要实现下载功能

数据库字段id为varchar2类型
name同上
path同上
sizes为bumber
type为varchar2类型
dates为date类型

java.lang.ClassCastException: java.io.FileInputStream cannot be cast to java.lang.String
at com.opensymphony.xwork2.DefaultActionInvocation.saveResult(DefaultActionInvocation.java:502) ~[xwork-core-2.3.28.1.jar:2.3.28.1]
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:465) ~[xwork-core-2.3.28.1.jar:2.3.28.1]

package com.javakc.action;

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

import com.javakc.dao.FileDaoimpl;
import com.javakc.entity.FileEntity;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {

private String id;
private String fileName;
public String execute() throws Exception
{
    System.out.println("开始执行文件下载操作!");
    return SUCCESS;
}

public InputStream getDownloadFile()
{
    //根据主键id获取当前记录
    FileEntity file=FileDaoimpl.load(id);
    fileName=file.getFileName();

    try {
        fileName=new String(fileName.getBytes("GBK"),"ISO8859-1");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //根据对象中路径信息获取文件
    File serverFile=new File(file.getFilePath());
    //将文件转为输入流
    InputStream input=null;

    try {
        input=new FileInputStream(serverFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return input;
}





public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getFileName() {
    return fileName;
}

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

}

2个回答

FileInputStream 不能直接转换为 String,可以这么做
http://www.cnblogs.com/qinpengming/p/6127336.html

qq_25990939
qq_25990939 我的这个代码怎么修改啊 搞了一下午了 麻烦帮忙贴出代码
2 年多之前 回复

InputStream inputStream = new FileInputStream("d:/sample.txt");
String text = IOUtils.toString(inputStream,"UTF-8");

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!