How to download .msi file in Java -


i want download .msi file using java. have tried download file using following code

printwriter out = null; fileinputstream filetodownload = null; bufferedreader bufferedreader = null; try {         out = response.getwriter();         filetodownload = new fileinputstream(download_directory + file_name);         bufferedreader = new bufferedreader(new inputstreamreader(filetodownload));          //response.setcontenttype("application/text");         //response.setcontenttype("application/x-msi");         //response.setcontenttype("application/msi");         //response.setcontenttype("octet-stream");         response.setcontenttype("application/octet-stream");         //response.setcontenttype("application/x-7z-compressed");         //response.setcontenttype("application/zip");         response.setheader("content-disposition","attachment; filename=" +file_name );         response.setcontentlength(filetodownload.available());          system.out.println("\n file download starting");         string nextline = "";         while((nextline = bufferedreader.readline()) != null){             out.println(nextline);         }         out.flush();                                      } catch (ioexception e) {         out.write("<center><h2>the installer not available on server</h2></center>");         system.out.println("\n got exception while getting input stream file==>"+e);         log.error("error::", e);     }     finally{         if(null != bufferedreader){             try {                 bufferedreader.close();             } catch (ioexception e) {                 system.out.println("\n error in closing buffer reader==>"+e);                 log.error("error::", e);             }         }// end of if          if(null != filetodownload){             try {                 filetodownload.close();             } catch (ioexception e) {                 system.out.println("\n error in closing input stream==>"+e);                 log.error("error::", e);             }         }// end of if     }// end of           

you can't read binary(msi) file readline() in case.your code totally wrong , not work. here simple function lets want.

    private void dodownload( httpservletrequest req, httpservletresponse resp,string filename, string original_filename )throws ioexception             {             file f = new file(filename);             int  length   = 0;             servletoutputstream op = resp.getoutputstream();             servletcontext context  = getservletconfig().getservletcontext();             string  mimetype = context.getmimetype( filename );             resp.setcontenttype( (mimetype != null) ? mimetype : "application/octet-stream" );             resp.setcontentlength( (int)f.length() );             resp.setheader( "content-disposition", "attachment; filename=\"" + original_filename + "\"" );             byte[] bbuf = new byte[bufsize];             datainputstream in = new datainputstream(new fileinputstream(f));             while ((in != null) && ((length = in.read(bbuf)) != -1)){             op.write(bbuf,0,length);             }             in.close();             op.flush();             op.close();         } 

create dodownload() function in servlet , pass requierd parameters function doget,dopost or whatever valid place like.

parameters:

  • @param req request

  • @param resp response

  • @param filename name of file want download.

  • @param original_filename name browser should receive.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -