码迷,mamicode.com
首页 > 其他好文 > 详细

ftp下载文件转化为输入流

时间:2016-04-08 14:42:23      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:

/**
	 * 取上传完整文件路径
	 * @param dir ftp定义的存储路径  例如 /jdyjs/jdyjs
	 * @param filename上传的文件名
	 * @return
	 * @throws Exception
	 */
	public static String getUploadFilePath(String dir, String filename) {
		byte[] bytes = null;
		Ydzf_UploadPdf ydzf_UploadPdf = new Ydzf_UploadPdf();
		FTPClient ftp = new FTPClient();
		ydzf_UploadPdf.initFtpServerParamsFromConfig();
		String path = "/"+dir+"/"+dir+"/"+filename;
		try {
			int reply;
			ftp.connect(ydzf_UploadPdf.getIp(), ydzf_UploadPdf.getPort());
			// 登录ftp
			ftp.login(ydzf_UploadPdf.getUsername(), ydzf_UploadPdf.getPassword());
			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
			}
			// 转到指定下载目录
			if (path != null) {//验证是否有该文件夹,有就转到,没有创建后转到该目录下  
				ftp.changeWorkingDirectory(path);//转到指定目录下
			}
			//2015/4/28 不需要遍历,改为直接用文件名取
			String remoteAbsoluteFile = toFtpFilename(path);
			InputStream in = null;
			// 下载文件
			ftp.setBufferSize(1024);
			ftp.setControlEncoding("UTF-8");
			ftp.setFileType(ftp.BINARY_FILE_TYPE);
			in = ftp.retrieveFileStream(remoteAbsoluteFile);
			bytes = input2byte(in);
			System.out.println("下载成功!" + bytes.length);
//			in.read(bytes);
			in.close();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return Base64.encodeBase64String(bytes);
	}
 /**
      * 文件转成 byte[] <一句话功能简述> <功能详细描述>
      * 
      * @param inStream
      * @return
      * @throws IOException
      * @see [类、类#方法、类#成员]
      */
	public static byte[] input2byte(InputStream inStream) throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[inStream.available()];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		swapStream.close();
		return in2b;
	}
	/**转化输出的编码*/
	private static String toFtpFilename(String fileName) throws Exception {
		return new String(fileName.getBytes("GBK"),"ISO8859-1");
	}

 

public class Ydzf_UploadPdf {
	private String ip;// ftp服务器ip
	private int port;
	private String username;
	private String password;
	private String path;
	private static final String FTPCONFIG = "ftpConfiguration.xml";
	public static final String FileDownloadTempDir = ServletActionContext.getServletContext().getRealPath("/wenshu_pdf/");
	public void initFtpServerParamsFromConfig() {
		String xmlPath = BaseAction.class.getClassLoader().getResource("/").getPath() + FTPCONFIG;
		if(xmlPath.contains("%20"))
			xmlPath = xmlPath.replaceAll("%20", " ");
		System.out.println(xmlPath);
		Document doc = Dom4jUtils.File2Document(xmlPath);
		Element root = doc.getRootElement();
		this.ip = root.element("ip").getText();
		this.port = Integer.valueOf(root.element("port").getText());
		this.username = root.element("username").getText();
		this.password = root.element("password").getText();
		this.path = root.element("path").getText();
	}

 

ftpConfiguration.xml
关于ftp的配置,可以在百度上查找很详细的 ftp默认的端口号是21,可以不去更改
<?xml version="1.0" encoding="UTF-8"?> <!-- 附件上传的FTP服务器配置 --> <ftpConfig> <ip>127.0.0.1</ip> <port>21</port> <username>ftpuser</username> <password>12345678</password> <path>/ftpServer</path> </ftpConfig>

 

ftp下载文件转化为输入流

标签:

原文地址:http://www.cnblogs.com/bingrong/p/5367631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!