0

I have a requirement to call a java code from my existing application code (spring boot), which will get multiple pdfs based on URLs provided and zip them and get them downloaded in my browser window. I have written below code,

package com.example.demo; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @WebServlet ( "/downloadServlet1" ) public class DownloadServlet1 extends HttpServlet { protected void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { System.out.println ( "reached here" ); List < String > fileUrls = List.of ( "http://www.w3.org/wai/er/tests/xhtml/testfiles/resources/pdf/dummy.pdf", "http://www.orimi.com/pdf-test.pdf" // Add more file URLs as needed ); System.out.println ( "reached here fileUrls " + fileUrls ); response.setContentType ( "application/zip" ); response.setHeader ( "Content-Disposition", "attachment; filename=\"download.zip\"" ); try (ZipOutputStream zipOutputStream = new ZipOutputStream ( response.getOutputStream ( ) )) { System.out.println ( "Inside zipOutputStream" ); for ( String fileUrl : fileUrls ) { System.out.println ( "Inside fileUrl " + fileUrl ); downloadFile ( fileUrl, zipOutputStream ); } } catch ( IOException e ) { e.printStackTrace ( ); // Handle exception appropriately } } private void downloadFile ( String fileUrl, ZipOutputStream zipOutputStream ) throws IOException { System.out.println ( "Inside download" ); try (InputStream in = new URL ( fileUrl ).openStream ( )) { String fileName = fileUrl.substring ( fileUrl.lastIndexOf ( '/' ) + 1 ); System.out.println ( "Inside fileName " + fileName ); zipOutputStream.putNextEntry ( new ZipEntry ( fileName ) ); byte[ ] buffer = new byte [ 2048 ]; int length; while ( ( length = in.read ( buffer ) ) > 0 ) { zipOutputStream.write ( buffer, 0, length ); System.out.println ( "writing done" ); } zipOutputStream.closeEntry ( ); } } } 

I am not getting any exception. getting all loggers till end and getting everything as successful, but still no file is getting downloaded in browser.

FYI, I invoking this servlet from my rest api controller as below,

@RestController public class ZipController { @GetMapping("/hello") public String testing() throws IOException { String downloadServletUrl = "http://localhost:8083/downloadServlet1"; try { URL url = new URL(downloadServletUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); System.out.println("value of connection "+ connection.getURL()); // Check response code and handle accordingly int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // File download was successful System.out.println("Files downloaded successfully."); } else { // Handle error System.err.println("Error downloading files. Response Code: " + responseCode); } } catch (IOException e) { e.printStackTrace(); // Handle exception appropriately } return "Testing zip"; } } 

I a getting "Testing zip" in browser output when hitting http://localhost:8083/hello which means api is not failing anywhere, still not getting zipped file as downloaded file.

Below are the logs :

 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.2.2)[0;39m [2m2024-02-09T17:52:16.413+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mc.e.demo.ZipFinalProject1Application [0;39m [2m:[0;39m Starting ZipFinalProject1Application using Java 17.0.9 with PID 32344 (C:\bag-manager\v6-project\git-project\ZipFinalProject-1\target\classes started by 10722114 in C:\bag-manager\v6-project\git-project\ZipFinalProject-1) [2m2024-02-09T17:52:16.415+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mc.e.demo.ZipFinalProject1Application [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" [2m2024-02-09T17:52:17.135+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8083 (http) [2m2024-02-09T17:52:17.146+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat] [2m2024-02-09T17:52:17.146+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.18] [2m2024-02-09T17:52:17.201+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext [2m2024-02-09T17:52:17.202+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 745 ms [2m2024-02-09T17:52:17.478+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8083 (http) with context path '' [2m2024-02-09T17:52:17.487+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[ main][0;39m [2m[0;39m[36mc.e.demo.ZipFinalProject1Application [0;39m [2m:[0;39m Started ZipFinalProject1Application in 1.36 seconds (process running for 2.037) [2m2024-02-09T17:52:22.573+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[nio-8083-exec-1][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet' [2m2024-02-09T17:52:22.573+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[nio-8083-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet' [2m2024-02-09T17:52:22.574+05:30[0;39m [32m INFO[0;39m [35m32344[0;39m [2m---[0;39m [2m[nio-8083-exec-1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 1 ms value of connection http://localhost:8083/downloadServlet1 reached here reached here fileUrls [http://www.w3.org/wai/er/tests/xhtml/testfiles/resources/pdf/dummy.pdf, http://www.orimi.com/pdf-test.pdf] Inside zipOutputStream Inside fileUrl http://www.w3.org/wai/er/tests/xhtml/testfiles/resources/pdf/dummy.pdf Inside download Inside fileName dummy.pdf Inside fileUrl http://www.orimi.com/pdf-test.pdf Inside download Inside fileName pdf-test.pdf writing done Files downloaded successfully. 
2
  • What is the content of the request body if you use your testing method? Commented Feb 9, 2024 at 12:29
  • I am not passing anything in request body. My target is to test this code inside DownloadServlet1. So I have written this ZipController so that I can invoke the logic written inside DownloadServlet1. Commented Feb 14, 2024 at 9:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.