Rose18 Rose18 -4 years ago 112
reST (reStructuredText) Question

Java: download CSV file with REST service

I am trying to download csv file from REST endpoint. Here is what I am trying.

@ApiOperation(value = "export",
notes = "Export Cache details for a given criteria")
@ApiImplicitParams({

})
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error") })
@RequestMapping(method = RequestMethod.GET, value = "/export")
public ResponseEntity export( HttpServletRequest request )
{
CacheDataManager cacheResultHandler = new CacheDataManager();
InputStreamResource inputStreamResource = null;
HttpHeaders httpHeaders = new HttpHeaders();
long contentLengthOfStream;

try
{
inputStreamResource = cacheResultHandler.exportCacheResults( request );
httpHeaders.set( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "test.csv" );

contentLengthOfStream = inputStreamResource.contentLength();
httpHeaders.setContentLength( contentLengthOfStream );
}
catch ( IOException e )
{
e.printStackTrace();
}

return new ResponseEntity( inputStreamResource, httpHeaders, HttpStatus.OK );

}


My export function.

@Override
public InputStreamResource export( HttpServletRequest request )
{
StringBuilder sb = new StringBuilder();
StringBuilder fileName = new StringBuilder( VALIDATION_REPORT );

sb.append( "Column A" );
sb.append( "," );
sb.append( "Column B" );
sb.append( "\n" );

try
{

sb.append( "TEST A");
sb.append( ',' );
sb.append( "TEST B" );
sb.append( '\n' );



fileName.append( "_" ).append( sdf.format( new Date() ) ).append( ".csv" );
return CsvFileWriter.csvFileWrite( fileName.toString(), sb );

}
catch ( Exception e )
{
e.printStackTrace();

}

return null;
}


CsvFileWriter.java

package it.app.ext.dashboard.util;

import org.springframework.core.io.InputStreamResource;

import java.io.*;

public class CsvFileWriter
{

public static InputStreamResource csvFileWrite( String fileName, StringBuilder content ) throws FileNotFoundException
{
File file = null;
PrintWriter pw = null;
try
{
file = new File( fileName );
pw = new PrintWriter( file );
pw.write( content.toString() );
}
catch ( FileNotFoundException e )
{
e.printStackTrace();
}
finally
{
pw.flush();
pw.close();
}

InputStream inputStream = new FileInputStream( file );

return new InputStreamResource( inputStream );
}


}

File is generating with content inside the tomcat/bin folder but exception occurred.

java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times.


I want to download a .csv file once call this endpoint.

Any suggestions are appreciated.

Thanks You

Answer Source

Explainations:

You got inputStream first:

contentLengthOfStream =inputStreamResource.contentLength();

Then Spring's returnValueHandlers got inputStream again:

new ResponseEntity( inputStreamResource, httpHeaders, HttpStatus.OK ).

But the inputStream wrapped by inputStreamResource only can be used once:

/**
 * This implementation throws IllegalStateException if attempting to
 * read the underlying stream multiple times.
 */
public InputStream getInputStream() throws IOException, IllegalStateException {
    if (this.read) {
        throw new IllegalStateException("InputStream has already been read - " +
                "do not use InputStreamResource if a stream needs to be read multiple times");
    }
    this.read = true;
    return this.inputStream;
}

Solution: You can get bytes from inputStream and return the ResponseEntity with bytes.

@ApiOperation(value = "export",
        notes = "Export Cache details for a given criteria")
@ApiImplicitParams({

})
@ApiResponses(value = {
        @ApiResponse(code = 400, message = "Bad Request"),
        @ApiResponse(code = 404, message = "Not Found"),
        @ApiResponse(code = 500, message = "Internal Server Error") })
@RequestMapping(method = RequestMethod.GET, value = "/export")
public ResponseEntity export( HttpServletRequest request )
{
    CacheDataManager cacheResultHandler = new CacheDataManager();
    InputStreamResource inputStreamResource = null;
    InputStream inputStream = null;
    byte[] byteArray = new byte[0];
    HttpHeaders httpHeaders = new HttpHeaders();

    try
    {
        inputStreamResource = cacheResultHandler.exportCacheResults( request );
        httpHeaders.set( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "test.csv" );

        //convert inputStream to bytes
        inputStream = inputStreamResource.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[1024];
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        buffer.flush();
        byteArray = buffer.toByteArray();

        httpHeaders.setContentLength(byteArray.length);
    }
    catch ( IOException e )
    {
        e.printStackTrace();
    }

    return new ResponseEntity( byteArray, httpHeaders, HttpStatus.OK );

}

Suggest: using Apache Commons IO to convert InputStream to bytes.Need to add a lib dependency,which can make your code brief

byte[] byteArray = IOUtils.toByteArray(inputStream);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download