Java implementation sftp download (with popup window)

Source: Internet
Author: User

Previous: First write, not quite. We all have a lot to forgive.

Recent projects, useful to the use of sftp download files, the Internet to find a circle of information: that is, Jsch file download is done by invoking the Get method of the Channelsftp object. (This is the reference Longyg blog) is generally get (file name, save address) This is silently go backstage download, no response, want to pop-up box that will Response.getoutputstream (). write (buffer);

The effect is that we normally download, the popup box asks where to save. (If your browser is set up, the browser to download the operation, one is to ask to save the location, and the second is to download the complete open file.) See your own settings)

Our project framework is Dubbo+springmvc+spring+mybatis

Controller Layer/** * Download log files*/@RequestMapping (Value="/download", method =requestmethod.get) @ResponseBody Public voidDownload (@RequestParam String fileName, httpservletrequest request, httpservletresponse response) throws Exception {sftputils sftp=NewSftputils (Host, A, account, PWD);//Virtual machine Test Here you write your own parameters.byte[] buffer =sftp.download (Logfileloadpath, fileName); Response.reset (); Response.setcontenttype ("Text/plain;charset=utf-8");//Text Formatif(Request.getheader ("user-agent"). toUpperCase (). IndexOf ("MSIE") >0){//handle the head information of IEResponse.setheader ("content-disposition","attachment;filename="+Urlencoder.encode (filename,charsets.utf_8.tostring ()));}Else{//handling Other header informationResponse.setheader ("content-disposition","attachment;filename="+NewString (Filename.getbytes (charsets.utf_8), charsets.iso_8859_1));} Response.getoutputstream (). write (buffer);}
Sftputil Tool Class Public classSftputils {protectedString host;protected intPort;protectedString username;protectedString password;/** * @param host ip* @param port port * @param username account * @param password Password **/ PublicSftputils (String host,intport, string Username, string password) {Set(host, port, username, password);} Public void Set(String host,intport, string Username, string password) { This. Host =host; This. Port =Port; This. Username =username; This. Password =password;} Session sshsession=NULL ;/** * Link to linux **/ Publicchannelsftp Connect () {channelsftp sftp=NULL ;Try{Jsch Jsch=NewJsch (); Jsch.getsession (username, host, port); Sshsession=jsch.getsession (username, host, port); Sshsession.setpassword (password); Properties Sshconfig=NewProperties (); Sshconfig.put ("stricthostkeychecking","No"); Sshsession.setconfig (sshconfig); Sshsession.connect (); Channel Channel= Sshsession.openchannel ("sftp"); Channel.connect (); SFTP=(CHANNELSFTP) channel;} Catch(Exception e) {Close (NULL );return NULL ;}returnsftp;}/** * Download files via sftp * * @Title: download* @param directory* @param downloadfile* @param savedirectory* @throws exception* @ret Run void* @author wangqinghua* @date 2015-9-24 pm 1:56:22*/ Public byte[] Download (string directory, String downloadFile) throws Exception {channelsftp sftp=Connect (); sftp.cd (directory); InputStream is= Sftp.Get(downloadFile);byte[] buffer = Inputstreamtobyte ( is); is. Close (); Close (SFTP);returnbuffer;}/** * InputStream converted to byte[]* @param is* @return byte[]* @throws ioexception*/Private byte[] Inputstreamtobyte (InputStream is) throws IOException {Bytearrayoutputstream Baoutputstream=NewBytearrayoutputstream ();intch; while(ch = is. read ())! =-1) {baoutputstream.write (ch);}bytedata [] =Baoutputstream.tobytearray (); Baoutputstream.close ();returndata;} Public voidClose (channelsftp sftp) {if(sftp!=NULL) {sftp.disconnect (); Sftp.exit ();}if(sshsession!=NULL) {sshsession.disconnect (); }}}

JS Layer:

<a class= ' detail link1 color-blue ' href=/logdetail/download?filename= "fileName"   //  This filename you want to get onclick= ' Window.location.href=this.href;return false; > Downloads </a>

Java implementation sftp download (with popup window)

Related Article

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.