Note: inputstream cannot be assigned between two different classes. It can only be obtained directly !!
Example of remote FTP download:
// Set this item to tell the servlet that the object is downloaded, not a webpage or TXT. (a dialog box is displayed)
Response. setcontenttype ("application/OCTET-STREAM ");
// When intputstream is used, use the following.
Servletoutputstream outputstream = response. getoutputstream ();
// The Problem of converting and downloading Chinese names.
String fpath = new string (filename. getbytes ("GBK"), "iso8859_1 ");
// Set the downloaded file name to be the same as the original file name.
Response. setheader ("content-disposition ",
"Attachment; filename = \" "+ fpath +
"\"");
System. Out. println (reffile. getfilename ());
Ftpclient = new ftpclient ();
// Connect to the FTP server
Ftpclient. Connection (severurl)
If (reffile! = NULL & (reffile. getfilesize () <1024*1024*10 )){
Temremote = new string ("\" + reffile. getfilename (). getbytes ("gb2312"), "iso-8859-1 ");
Inputstream = ftpclient. retrievefilestream (temremote );
System. Out. println ("00001 ");
}
Else if (reffile! = NULL & (reffile. getfilesize ()> 1024*1024*10 )){
Temremote = new string (reffile. getmetaurl () + "\" +
Reffile. getfilename (). getbytes ("gb2312"), "iso-8859-1 ");
Inputstream = ftpclient. retrievefilestream (temremote );
}
System. Out. println ("<10m =" + "\" + reffile. getfilename ());
System. Out. println ("> 10 m =" + reffile. getmetaurl () + "\" +
Reffile. getfilename ());
Int I;
Int chunk = inputstream. Available ();
System. Out. println ("chunk data length =" + chunk );
// The byte array accepts the file data
Byte [] buffer = new byte [chunk];
Int length =-1;
If (inputstream = NULL ){
System. Out. println ("the input stream is empty !! ");
}
While (length = inputstream. Read (buffer ))! =-1 ){
System. Out. println ("length Data Length =" + length );
Outputstream. Write (buffer, 0, length); // read the stream and save it in the byte array
}
Inputstream. Close ();
Outputstream. Flush ();
Outputstream. Close ();
}