0

我想使用 Java 下载 .msi 文件。我尝试使用以下代码下载文件

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 now file download is starting");
        String NextLine = "";
        while((NextLine = bufferedReader.readLine()) != null){
            out.println(NextLine);
        }
        out.flush();                                

    } catch (IOException e) {
        out.write("<center><h2>The Installer is not Available on Server</h2></center>");
        System.out.println("\n Got Exception while getting the input Stream from the 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 finally          
4

1 回答 1

1

在这种情况下,您无法读取二进制(msi)文件readline()。您的代码完全错误,无法正常工作。这是一个简单的功能,可让您做您想做的事。

    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();
        }

在您的函数中创建doDownload()函数servlet并将所需参数从 或任何您喜欢的有效位置传递给该doGet函数doPost

参数:

  • @param req 请求

  • @param resp 响应

  • @param filename 要下载的文件的名称。

  • @param original_filename 浏览器应该接收的名称。

于 2012-06-29T11:11:02.670 回答