qq_32016061 2018-10-15 01:50
浏览 360

通过Struts2下载ftp图片文件,图片花屏。不知道怎么回事

图片说明

在ftp上图片是正常的,下载后就是这个样子。

这是下载文件部分代码

 @ParentPackage("framework-default")
@Action(value = "ueditorController", results = { @org.apache.struts2.convention.annotation.Result(name = { "success" }, type = "stream", params = {
        "contentType", "application/octet-stream", "inputName",
        "fileInputStream", "contentDisposition", "attachment;filename=${name}" }) })
public class UeditorControllerAction extends ActionSupport {
    private static final long serialVersionUID = -271242185203136724L;
    private File[] upfile; // 文件名
    private String[] upfileFileName;
    private String[] upfileContentType;// 文件类型
    private String path;
    private String type;
    private String upfileType;
    private int size; // 文件大小
    private String name;

    public String downloadFile() throws Exception {
        // if (StringUtils.isEmpty(name)) {
        // name = new String(URLDecoder.decode(
        // URLDecoder.decode(name, "utf-8"), "utf-8")
        // .getBytes("UTF-8"), "ISO-8859-1");// 进行转码,必须两次编码和两次解码才能保证不同服务器上均不乱码
        // } else {
        // name = path;
        // }
        RichTextConfig config = RichTextConfig.getInstance();
        if (config == null) {
            LogUtil.getFrameworkLoger().error("没有找到图片上传的ftp服务器");
        }
        FTPFileHandler handler = new FTPFileHandler();
        try {
            handler.connect(config.getHost(), config.getUser(),
                    config.getPassword(), config.getPort());
        } catch (Exception e) {
            LogUtil.getFrameworkLoger().error("连接ftp服务器失败", e);
        }
        InputStream inputstream = null;
        try {
            inputstream = handler.downloadInputStream(path, "");
        } catch (Exception e) {
            LogUtil.getFrameworkLoger().error("下载文件失败", e);
        }
        ActionContext.getContext().getValueStack()
                .set("fileInputStream", inputstream);
        return "success";
    }


}

获取文件流代码

 /**
     *下载文件流
     * @param remoteFileName 文件名
     * @param path 路径 
     */
    public InputStream downloadInputStreamForFTP(String remoteFileName, String path) {
        InputStream in = null;
        try {
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.changeWorkingDirectory(path);
            ftpClient.enterLocalPassiveMode();
            in = ftpClient.retrieveFileStream(remoteFileName);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("服务器不存该目录",e);
        } catch (IOException e) {
            throw new RuntimeException("下载的文件不存在",e);
        }
        return in;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办