In Rest API, need to construct ZIP and download to client system





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a REST API, which need to :
1. Create a few Json files from objects
2. Zip all of them
3. Download to client system - whoever is accessing the URL.



I am able to do step 1 and 2, but on doing step 3, it is downloading to my server, but not to client.
Currently i am trying in my local, Later this code moves to AWS, not sure there also it will download into the server or client local:



Code for step 3:



public void downloadZip(String zipFileName)
{
File file = new File(zipFileName);
if(!file.exists()){
System.out.println("file not found");
}

OutputStream out = null;
try
{
out = new FileOutputStream("test_"+zipFileName);
FileInputStream in = new FileInputStream(file);
byte buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}









share|improve this question























  • You code is just reading a file and writing it to another file

    – Scary Wombat
    Nov 22 '18 at 2:01











  • Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

    – Scary Wombat
    Nov 22 '18 at 2:02











  • Possible duplicate of Download a zip file using Restful services

    – htpvl
    Nov 22 '18 at 2:07


















0















I have a REST API, which need to :
1. Create a few Json files from objects
2. Zip all of them
3. Download to client system - whoever is accessing the URL.



I am able to do step 1 and 2, but on doing step 3, it is downloading to my server, but not to client.
Currently i am trying in my local, Later this code moves to AWS, not sure there also it will download into the server or client local:



Code for step 3:



public void downloadZip(String zipFileName)
{
File file = new File(zipFileName);
if(!file.exists()){
System.out.println("file not found");
}

OutputStream out = null;
try
{
out = new FileOutputStream("test_"+zipFileName);
FileInputStream in = new FileInputStream(file);
byte buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}









share|improve this question























  • You code is just reading a file and writing it to another file

    – Scary Wombat
    Nov 22 '18 at 2:01











  • Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

    – Scary Wombat
    Nov 22 '18 at 2:02











  • Possible duplicate of Download a zip file using Restful services

    – htpvl
    Nov 22 '18 at 2:07














0












0








0








I have a REST API, which need to :
1. Create a few Json files from objects
2. Zip all of them
3. Download to client system - whoever is accessing the URL.



I am able to do step 1 and 2, but on doing step 3, it is downloading to my server, but not to client.
Currently i am trying in my local, Later this code moves to AWS, not sure there also it will download into the server or client local:



Code for step 3:



public void downloadZip(String zipFileName)
{
File file = new File(zipFileName);
if(!file.exists()){
System.out.println("file not found");
}

OutputStream out = null;
try
{
out = new FileOutputStream("test_"+zipFileName);
FileInputStream in = new FileInputStream(file);
byte buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}









share|improve this question














I have a REST API, which need to :
1. Create a few Json files from objects
2. Zip all of them
3. Download to client system - whoever is accessing the URL.



I am able to do step 1 and 2, but on doing step 3, it is downloading to my server, but not to client.
Currently i am trying in my local, Later this code moves to AWS, not sure there also it will download into the server or client local:



Code for step 3:



public void downloadZip(String zipFileName)
{
File file = new File(zipFileName);
if(!file.exists()){
System.out.println("file not found");
}

OutputStream out = null;
try
{
out = new FileOutputStream("test_"+zipFileName);
FileInputStream in = new FileInputStream(file);
byte buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}






java rest file download zip






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 1:56









vasuvasu

416




416













  • You code is just reading a file and writing it to another file

    – Scary Wombat
    Nov 22 '18 at 2:01











  • Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

    – Scary Wombat
    Nov 22 '18 at 2:02











  • Possible duplicate of Download a zip file using Restful services

    – htpvl
    Nov 22 '18 at 2:07



















  • You code is just reading a file and writing it to another file

    – Scary Wombat
    Nov 22 '18 at 2:01











  • Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

    – Scary Wombat
    Nov 22 '18 at 2:02











  • Possible duplicate of Download a zip file using Restful services

    – htpvl
    Nov 22 '18 at 2:07

















You code is just reading a file and writing it to another file

– Scary Wombat
Nov 22 '18 at 2:01





You code is just reading a file and writing it to another file

– Scary Wombat
Nov 22 '18 at 2:01













Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

– Scary Wombat
Nov 22 '18 at 2:02





Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example

– Scary Wombat
Nov 22 '18 at 2:02













Possible duplicate of Download a zip file using Restful services

– htpvl
Nov 22 '18 at 2:07





Possible duplicate of Download a zip file using Restful services

– htpvl
Nov 22 '18 at 2:07












1 Answer
1






active

oldest

votes


















0














Try this, using content type application/zip.



 if(file.exists()){
byte arr = FileUtils.readFileToByteArray(file);
return ResponseEntity.ok().contentLength(arr.length)
.header(HttpHeaders.CONTENT_TYPE, "application/zip")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
.body(arr);
} else {
return ResponseEntity.notFound().build();
}





share|improve this answer
























  • Is this a Spring solution?

    – Scary Wombat
    Nov 22 '18 at 2:05











  • @Scary, yes, it is :-)

    – htpvl
    Nov 22 '18 at 2:06











  • I wonder if the OP is using Spring - maybe eh.

    – Scary Wombat
    Nov 22 '18 at 2:07











  • @Scary, I just found it is duplicated question. Flagged it already ;-)

    – htpvl
    Nov 22 '18 at 2:08






  • 1





    Yeah, but I prefer your answer, but wish it had pointers for using without Spring

    – Scary Wombat
    Nov 22 '18 at 2:13












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422866%2fin-rest-api-need-to-construct-zip-and-download-to-client-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try this, using content type application/zip.



 if(file.exists()){
byte arr = FileUtils.readFileToByteArray(file);
return ResponseEntity.ok().contentLength(arr.length)
.header(HttpHeaders.CONTENT_TYPE, "application/zip")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
.body(arr);
} else {
return ResponseEntity.notFound().build();
}





share|improve this answer
























  • Is this a Spring solution?

    – Scary Wombat
    Nov 22 '18 at 2:05











  • @Scary, yes, it is :-)

    – htpvl
    Nov 22 '18 at 2:06











  • I wonder if the OP is using Spring - maybe eh.

    – Scary Wombat
    Nov 22 '18 at 2:07











  • @Scary, I just found it is duplicated question. Flagged it already ;-)

    – htpvl
    Nov 22 '18 at 2:08






  • 1





    Yeah, but I prefer your answer, but wish it had pointers for using without Spring

    – Scary Wombat
    Nov 22 '18 at 2:13
















0














Try this, using content type application/zip.



 if(file.exists()){
byte arr = FileUtils.readFileToByteArray(file);
return ResponseEntity.ok().contentLength(arr.length)
.header(HttpHeaders.CONTENT_TYPE, "application/zip")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
.body(arr);
} else {
return ResponseEntity.notFound().build();
}





share|improve this answer
























  • Is this a Spring solution?

    – Scary Wombat
    Nov 22 '18 at 2:05











  • @Scary, yes, it is :-)

    – htpvl
    Nov 22 '18 at 2:06











  • I wonder if the OP is using Spring - maybe eh.

    – Scary Wombat
    Nov 22 '18 at 2:07











  • @Scary, I just found it is duplicated question. Flagged it already ;-)

    – htpvl
    Nov 22 '18 at 2:08






  • 1





    Yeah, but I prefer your answer, but wish it had pointers for using without Spring

    – Scary Wombat
    Nov 22 '18 at 2:13














0












0








0







Try this, using content type application/zip.



 if(file.exists()){
byte arr = FileUtils.readFileToByteArray(file);
return ResponseEntity.ok().contentLength(arr.length)
.header(HttpHeaders.CONTENT_TYPE, "application/zip")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
.body(arr);
} else {
return ResponseEntity.notFound().build();
}





share|improve this answer













Try this, using content type application/zip.



 if(file.exists()){
byte arr = FileUtils.readFileToByteArray(file);
return ResponseEntity.ok().contentLength(arr.length)
.header(HttpHeaders.CONTENT_TYPE, "application/zip")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
.body(arr);
} else {
return ResponseEntity.notFound().build();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 2:02









htpvlhtpvl

647310




647310













  • Is this a Spring solution?

    – Scary Wombat
    Nov 22 '18 at 2:05











  • @Scary, yes, it is :-)

    – htpvl
    Nov 22 '18 at 2:06











  • I wonder if the OP is using Spring - maybe eh.

    – Scary Wombat
    Nov 22 '18 at 2:07











  • @Scary, I just found it is duplicated question. Flagged it already ;-)

    – htpvl
    Nov 22 '18 at 2:08






  • 1





    Yeah, but I prefer your answer, but wish it had pointers for using without Spring

    – Scary Wombat
    Nov 22 '18 at 2:13



















  • Is this a Spring solution?

    – Scary Wombat
    Nov 22 '18 at 2:05











  • @Scary, yes, it is :-)

    – htpvl
    Nov 22 '18 at 2:06











  • I wonder if the OP is using Spring - maybe eh.

    – Scary Wombat
    Nov 22 '18 at 2:07











  • @Scary, I just found it is duplicated question. Flagged it already ;-)

    – htpvl
    Nov 22 '18 at 2:08






  • 1





    Yeah, but I prefer your answer, but wish it had pointers for using without Spring

    – Scary Wombat
    Nov 22 '18 at 2:13

















Is this a Spring solution?

– Scary Wombat
Nov 22 '18 at 2:05





Is this a Spring solution?

– Scary Wombat
Nov 22 '18 at 2:05













@Scary, yes, it is :-)

– htpvl
Nov 22 '18 at 2:06





@Scary, yes, it is :-)

– htpvl
Nov 22 '18 at 2:06













I wonder if the OP is using Spring - maybe eh.

– Scary Wombat
Nov 22 '18 at 2:07





I wonder if the OP is using Spring - maybe eh.

– Scary Wombat
Nov 22 '18 at 2:07













@Scary, I just found it is duplicated question. Flagged it already ;-)

– htpvl
Nov 22 '18 at 2:08





@Scary, I just found it is duplicated question. Flagged it already ;-)

– htpvl
Nov 22 '18 at 2:08




1




1





Yeah, but I prefer your answer, but wish it had pointers for using without Spring

– Scary Wombat
Nov 22 '18 at 2:13





Yeah, but I prefer your answer, but wish it had pointers for using without Spring

– Scary Wombat
Nov 22 '18 at 2:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422866%2fin-rest-api-need-to-construct-zip-and-download-to-client-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)