FTP download file into input stream

Source: Internet
Author: User

/** * Take upload full file path * @param dir FTP defined storage path such as/jdyjs/jdyjs * @param filename uploaded file name * @return * @throws Exception */public St Atic 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 ());//Login Ftpftp.login (Ydzf_uploadpdf.getusername (), Ydzf_uploadpdf.getpassword ()); Ftp.setfiletype ( Ftpclient.binary_file_type); reply = Ftp.getreplycode (); if (! Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();} Go to the specified download directory if (path! = null) {///verify if there is a folder, go to it, do not create and go to the directory under Ftp.changeworkingdirectory (path);//go to the specified directory}//2015/4/28 Does not need to traverse, instead directly uses the file name to take string remoteabsolutefile = Toftpfilename (path); InputStream in = null;//download file ftp.setbuffersize (1024); Ftp.setcontrolencoding ("UTF-8"); Ftp.setfiletype (ftp.binary_file_type); in = Ftp.retrievefilestream (remoteabsolutefile); bytes = Input2byte (in); System.out.println ("Download succeeded!" + bytes.length);//in.read (bytes); In.close ();} catch (SocketException e) {e.printstacktrace (),} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {E.prin Tstacktrace ();} Return base64.encodebase64string (bytes);}
/**      * file to byte[] < A word function description > < function details > * *       @param instream      * @return      * @throws IOException      * @see [Class, Class # method, Class # member]      */public static byte[] Input2byte (InputStream instream) throws IOException { Bytearrayoutputstream Swapstream = new Bytearrayoutputstream (); byte[] buff = new byte[instream.available ()];int rc = 0;wh Ile (rc = instream.read (buff, 0,)) > 0) {swapstream.write (buff, 0, RC);} byte[] in2b = Swapstream.tobytearray (); Swapstream.close (); return in2b;} The/** conversion output is encoded */private static string Toftpfilename (String fileName) throws Exception {return new String (Filename.getbytes ( "GBK"), "iso8859-1");}

public class Ydzf_uploadpdf {private String ip;//FTP server ipprivate int port;private string username;private string password ;p rivate string path;private static final String ftpconfig = "Ftpconfiguration.xml";p ublic static final string Filedownloa Dtempdir = Servletactioncontext.getservletcontext (). Getrealpath ("/wenshu_pdf/");p ublic Void Initftpserverparamsfromconfig () {String Xmlpath = BaseAction.class.getClassLoader (). GetResource ("/"). GetPath () + Ftpconfig;if (Xmlpath.contains ("%20")) Xmlpath = Xmlpath.replaceall ("%20", ""); System.out.println (Xmlpath);D ocument 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
About the configuration of FTP, you can find in Baidu very detailed FTP default port number is 21, you can not change
<?xml version= "1.0" encoding= "UTF-8"?><!--attachments uploaded FTP server configuration--><ftpconfig><ip>127.0.0.1</ Ip><port>21</port><username>ftpuser</username><password>12345678</password ><path>/ftpServer</path></ftpConfig>

FTP download file into input stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.