In Rest API, need to construct ZIP and download to client system
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
java rest file download zip
asked Nov 22 at 1:56
vasu
416
416
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07
add a comment |
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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();
}
Is this aSpring
solution?
– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is usingSpring
- maybe eh.
– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
Yeah, but I prefer your answer, but wish it had pointers for using withoutSpring
– Scary Wombat
Nov 22 at 2:13
|
show 5 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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();
}
Is this aSpring
solution?
– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is usingSpring
- maybe eh.
– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
Yeah, but I prefer your answer, but wish it had pointers for using withoutSpring
– Scary Wombat
Nov 22 at 2:13
|
show 5 more comments
up vote
0
down vote
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();
}
Is this aSpring
solution?
– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is usingSpring
- maybe eh.
– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
Yeah, but I prefer your answer, but wish it had pointers for using withoutSpring
– Scary Wombat
Nov 22 at 2:13
|
show 5 more comments
up vote
0
down vote
up vote
0
down vote
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();
}
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();
}
answered Nov 22 at 2:02
htpvl
62539
62539
Is this aSpring
solution?
– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is usingSpring
- maybe eh.
– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
Yeah, but I prefer your answer, but wish it had pointers for using withoutSpring
– Scary Wombat
Nov 22 at 2:13
|
show 5 more comments
Is this aSpring
solution?
– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is usingSpring
- maybe eh.
– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
Yeah, but I prefer your answer, but wish it had pointers for using withoutSpring
– Scary Wombat
Nov 22 at 2:13
Is this a
Spring
solution?– Scary Wombat
Nov 22 at 2:05
Is this a
Spring
solution?– Scary Wombat
Nov 22 at 2:05
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
@Scary, yes, it is :-)
– htpvl
Nov 22 at 2:06
I wonder if the OP is using
Spring
- maybe eh.– Scary Wombat
Nov 22 at 2:07
I wonder if the OP is using
Spring
- maybe eh.– Scary Wombat
Nov 22 at 2:07
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
@Scary, I just found it is duplicated question. Flagged it already ;-)
– htpvl
Nov 22 at 2:08
1
1
Yeah, but I prefer your answer, but wish it had pointers for using without
Spring
– Scary Wombat
Nov 22 at 2:13
Yeah, but I prefer your answer, but wish it had pointers for using without
Spring
– Scary Wombat
Nov 22 at 2:13
|
show 5 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You code is just reading a file and writing it to another file
– Scary Wombat
Nov 22 at 2:01
Have a look at codejava.net/java-ee/servlet/java-servlet-download-file-example
– Scary Wombat
Nov 22 at 2:02
Possible duplicate of Download a zip file using Restful services
– htpvl
Nov 22 at 2:07