zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    centos7.6 使用yum安装mysql5.7
    解决hadoop本地库问题
    docker-compose 启动警告
    docker 安装zabbix5.0 界面乱码问题解决
    docker 部署zabbix问题
    zookeeper 超时问题
    hbase regionserver异常宕机
    (转载)hadoop 滚动升级
    hadoop Requested data length 86483783 is longer than maximum configured RPC length
    zkfc 异常退出问题,报错Received stat error from Zookeeper. code:CONNECTIONLOSS
  • 原文地址:https://www.cnblogs.com/bingrong/p/5367631.html
Copyright © 2011-2022 走看看