ftp下載文件轉化為輸入流


/**
	 * 取上傳完整文件路徑
	 * @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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM