话不多说,上代码
public static FTPClientForTree getInstance() {
if (ftpClientUtil == null) {
ftpClientUtil = new FTPClientForTree();
}
return ftpClientUtil;
}
FTPClientForTree.getInstance().
viewImage(orderFileReport.getFilePath(),orderFileReport.getFileName(),response);
通过上面这行代码访问
filePath为路径,fileName文件名
FTP_ADDRESS,FTP_PONT分别是ip和端口号
FTP_USERNAME, FTP_PASSWORD 分别是账号和密码
图片
//预览图片
public String viewImage(String filePath, String fileName, HttpServletResponse response) {
String result = "";
FTPClient ftpClient = new FTPClient();
InputStream is = null;
BufferedInputStream bis = null;
try {
if(fileName.contains("png")) {
response.setContentType("image/png");
} else if(fileName.contains("gif")) {
response.setContentType("image/gif");
} else if(fileName.contains("jpg")) {
response.setContentType("image/jpeg");
}
response.setHeader("content-disposition", "inline;filename="+URLEncoder.encode(fileName, "UTF-8"));
ftpClient.connect(FTP_ADDRESS,FTP_PONT);
ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
ftpClient.setBufferSize(1024);
// 设置文件类型
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
String newPath = new String(filePath.getBytes("GBK"),"ISO-8859-1");
if (ftpClient.isConnected()) {
is = ftpClient.retrieveFileStream(newPath);
if(is != null){
bis = new BufferedInputStream(is);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) > 0) {
out.write(buf,0,len);
}
ftpClient.logout();
result = "FTP文件下载成功";
return result;
}
}
} catch (IOException e) {
e.printStackTrace();
//result = "FTP瀹㈡埛绔嚭閿�";
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
//result = "鍏抽棴FTP杩炴帴鍙戠敓寮傚父";
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
//预览pdf
public String viewPDF(String filePath, String fileName, HttpServletResponse response) {
String result = "";
FTPClient ftpClient = new FTPClient();
InputStream is = null;
BufferedInputStream bis = null;
try {
response.setContentType("application/pdf");
response.setHeader("content-disposition", "inline;filename="+URLEncoder.encode(fileName, "UTF-8"));
ftpClient.connect(FTP_ADDRESS,FTP_PONT);
ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
ftpClient.setBufferSize(1024);
// 设置文件类型
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
String newPath = new String(filePath.getBytes("GBK"),"ISO-8859-1");
if (ftpClient.isConnected()) {
is = ftpClient.retrieveFileStream(newPath);
if(is != null){
bis = new BufferedInputStream(is);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) > 0) {
out.write(buf,0,len);
}
ftpClient.logout();
result = "FTP文件下载成功";
return result;
}
}
} catch (IOException e) {
e.printStackTrace();
//result = "FTP瀹㈡埛绔嚭閿�";
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
//result = "鍏抽棴FTP杩炴帴鍙戠敓寮傚父";
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}