在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;
}