在Servlet中下载文件

问题描述 投票:0回答:1

我试图将Servlet中的文件下载到Web客户端。我继续得到这个错误。

Exception in thread "Thread-5" java.lang.NullPointerException
at org.apache.coyote.http11.Http11OutputBuffer.commit(Http11OutputBuffer.java:347)
at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1402)
at org.apache.coyote.AbstractProcessor.action(AbstractProcessor.java:327)
at org.apache.coyote.Response.action(Response.java:173)
at org.apache.coyote.http11.Http11OutputBuffer.doWrite(Http11OutputBuffer.java:219)
at org.apache.coyote.Response.doWrite(Response.java:541)
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:351)
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:825)
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:730)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:391)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:369)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)

这是我下载文件的代码代码。 3次迭代后,该行发生错误:os.write(bufferData, 0, read);

        File desktop = File.createTempFile("dump",".csv");
    mLink = desktop.getAbsolutePath();
    FileWriter writer = new FileWriter(desktop);
    mMessage = "Loading data to CSV to ";
    ICsvListWriter csvWriter = null;
    try {
        csvWriter = new CsvListWriter(writer,
                CsvPreference.STANDARD_PREFERENCE);

        for (int i = 0; i < csvMatrix.size(); i++) {
            csvWriter.write(csvMatrix.get(i));
        }

    } catch (IOException e) {
        e.printStackTrace(); // TODO handle exception properly
        mMessage = e.getLocalizedMessage();
    } finally {
        try {
            csvWriter.close();


        } catch (IOException e) {
            e.printStackTrace();
            mMessage = e.getLocalizedMessage();
        }
    }

    // reads input file from an absolute path
    String fileName =mLink;
    if(fileName == null || fileName.equals("")){
        throw new ServletException("File Name can't be null or empty");
    }
    File file = new File(mLink);
    if(!file.exists()){
        throw new ServletException("File doesn't exists on server.");
    }
    System.out.println("File location on server::"+file.getAbsolutePath());
    ServletContext ctx = getServletContext();
    InputStream fis = new FileInputStream(file);
    String mimeType = ctx.getMimeType(file.getAbsolutePath());
    mResponce.setContentType(mimeType != null? mimeType:"application/octet-stream");
    mResponce.setContentLength((int) file.length());
    mResponce.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    ServletOutputStream os       = mResponce.getOutputStream();
    byte[] bufferData = new byte[1024];
    int read=0;
    while((read = fis.read(bufferData))!= -1){
        os.write(bufferData, 0, read);
    }
    os.flush();
    os.close();
    fis.close();
    System.out.println("File downloaded at client successfully");
java file servlets download supercsv
1个回答
0
投票

根据你,如果错误在这个代码中

ServletOutputStream os       = mResponce.getOutputStream();
    byte[] bufferData = new byte[1024];
    int read=0;
    while((read = fis.read(bufferData))!= -1){
        os.write(bufferData, 0, read);
    }

然后尝试这个

    OutputStream out = mResponce.getOutputStream();
int i;
        while ((i = fis.read()) != -1) {
            out.write(i);
        }
fis.close()
© www.soinside.com 2019 - 2024. All rights reserved.