Скачать файл в сервлете - PullRequest
0 голосов
/ 02 июля 2018

Я пытаюсь загрузить файл в сервлете в веб-клиент. Я продолжаю получать эту ошибку.

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");

1 Ответ

0 голосов
/ 02 июля 2018

По вашему мнению, если в этом коде ошибка

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...