访问登录linux预览文件,包括图片,pdf,word

话不多说,上代码

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

//预览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;
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值