Send Large Files like ISO via Java REST -


i creating rest service send large files such iso image,but getting out of memory error,below code

@requestmapping(value = uriconstansts.get_file, produces = { "application/json" }, method = requestmethod.get) public @responsebody responseentity getfile(@requestparam(value="filename", required=false) string filename,httpservletrequest request) throws ioexception{      responseentity respentity = null;      byte[] reportbytes = null;     file result=new file("/home/xxx/xxx/xxx/dummypath/"+filename);      if(result.exists()){         inputstream inputstream = new fileinputstream("/home/xxx/xxx/xxx/dummypath/"+filename);          string type=result.tourl().openconnection().guesscontenttypefromname(filename);          byte[]out=org.apache.commons.io.ioutils.tobytearray(inputstream);          httpheaders responseheaders = new httpheaders();         responseheaders.add("content-disposition", "attachment; filename=" + filename);         responseheaders.add("content-type",type);          respentity = new responseentity(out, responseheaders,httpstatus.ok);       }else{          respentity = new responseentity ("file not found", httpstatus.ok);     }       return respentity;  } 

it looks need use inputstreamresource returning stream instead of whole file @ once.

      package app.controller;      import org.springframework.core.io.inputstreamresource;     import org.springframework.http.httpheaders;     import org.springframework.http.httpstatus;     import org.springframework.http.mediatype;     import org.springframework.http.responseentity;     import org.springframework.web.bind.annotation.requestmapping;     import org.springframework.web.bind.annotation.requestmethod;     import org.springframework.web.bind.annotation.requestparam;      import java.io.file;     import java.io.fileinputstream;     import java.io.filenotfoundexception;      @org.springframework.web.bind.annotation.restcontroller     @requestmapping(path = {"/rest"})     public class restcontroller {          @requestmapping(value = "/getiso", method = requestmethod.get, produces = mediatype.application_octet_stream_value)         public responseentity getisofile(@requestparam(value="filepath", required = true) string filepath) throws filenotfoundexception {             file file = new file(filepath);             fileinputstream inputstream = new fileinputstream(file);             inputstreamresource inputstreamresource = new inputstreamresource(inputstream);             httpheaders httpheaders = new httpheaders();             httpheaders.setcontentlength((int)file.length());             return new responseentity(inputstreamresource, httpheaders, httpstatus.ok);         }     }  

also notice, should use in case mediatype.application_octet_stream_value instead of application/json produced type.

hope helps.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -