Note that there are some explanatory texts on larger screens.

plurals
  1. PODownload a file through Spring MVC controller using streams
    primarykey
    data
    text
    <p>I am using spring MVC with REST service for one of my project. I am having a service to attach and download user files. I am using below service API for upload and save file into server directory</p> <pre><code>http://myrestserver/attachmentService/attach/userKey </code></pre> <p>And below service API for download files from server directory</p> <pre><code>http://myrestserver/attachmentService/download/userKey/fileKey </code></pre> <p>The issue is that when a file is downloaded, the downloaded URL shows the REST service API URL. To avoid this, I thought of write a controller for attach and download file.</p> <p>I wrote a spring controller which handle file attachment process. Even I wrote a controller(say download.do) for download a file, but when a file downloaded, the file name shows as the same name of the controller(downloaded file name shows <code>"download.do"</code> always) instead of original file name. </p> <p>Below code is from my <code>download.do</code> controller</p> <pre><code>WebResource resource = null; resource = client.resource("http://myrestserver/attachmentService/download/userKey/fileKey"); clientResponse = resource.accept(MediaType.APPLICATION_OCTET_STREAM).get( ClientResponse.class); InputStream inputStream = clientResponse.getEntityInputStream(); if(inputStream != null){ byteArrayOutputStream = new ByteArrayOutputStream(); try { IOUtil.copyStream(inputStream, byteArrayOutputStream); } catch (IOException e) { log.error("Exception in download:"+ e); } } </code></pre> <p>And, in my service API, the code is </p> <pre><code>file = new File(directory, attachmentFileName); fileOutputStream = new FileOutputStream(file); fileOutputStream.write(attachmentContent); fileOutputStream.close(); response = Response.ok((Object) file).type(MediaType.APPLICATION_OCTET_STREAM); response.header("Content-Disposition", "attachment; filename=" + "\"" + attachmentFileName + "\""); return response.build(); </code></pre> <p>By analyzing the issue, I understood that, am not setting file header in downloaded file through <code>download.do</code> controller. If I am using outstream in <code>download.do</code> controller, I will not be able to set the file header. </p> <p>Can any one help me to resolve this issue. My primary aim is to hide my rest service URL from downloaded file by stream through a MVC controller. I found a post (<a href="https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers">Downloading a file from spring controllers</a> )in stack overflow almost like my question, but the file type is previously known. Please note that, in my application user can attach any type of file.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload