@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void getFile( @PathVariable("file_name") String fileName, HttpServletResponse response) { try { // get your file as InputStream InputStream is = ...; // copy it to response's OutputStream org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { log.info("Error writing file to output stream. Filename was '{}'", fileName, ex); throw new RuntimeException("IOError writing file to output stream"); } }
Generally speaking, when you have response.getOutputStream()
, you can write anything there. You can pass this output stream as a place to put generated PDF to your generator. Also, if you know what file type you are sending, you can set
response.setContentType("application/pdf");
Related Posts:
- Understanding Spring @Autowired usage
- com.atomikos.icatch.SysException: Error in init: Log already in use? tmlog in ./
- Spring @PropertySources value not overriding
- How do I save a String to a text file using Java?
- HTTP Status 405 – Request method ‘POST’ not supported (Spring MVC)
- “NoClassDefFoundError: Could not initialize class” error
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘MyController’:
- Error creating bean with name ‘entityManagerFactory’ defined in class path resource : Invocation of init method failed
- No String-argument constructor/factory method to deserialize from String value (”)
- Spring boot – Request method ‘POST’ not supported. Tried everything
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Error creating bean with name ‘entityManagerFactory’ defined in class path resource : Invocation of init method failed
- What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
- Difference between spring @Controller and @RestController annotation
- Unable to create requested service [org.hibernate .engine.jdbc.env.spi.JdbcEnvironment]-MySQL
- What is the best/simplest way to read in an XML file in Java application?
- @Autowired – No qualifying bean of type found for dependency
- Rename a file using Java
- HTTP Status 405 – Request method ‘POST’ not supported (Spring MVC)
- How to resolve Error listenerStart when deploying web-app in Tomcat 5.5?
- The system cannot find the file specified in java
- com.sun.jdi.InvocationException occurred invoking method
- UnsatisfiedDependencyException: Error creating bean with name
- UnsatisfiedDependencyException: Error creating bean with name
- Only using @JsonIgnore during serialization, but not deserialization
- Spring Maven clean error – The requested profile “pom.xml” could not be activated because it does not exist
- How/where to download openjdk/openjre for windows [duplicate]
- Why SpringMVC Request method ‘GET’ not supported?
- intellij incorrectly saying no beans of type found for autowired repository
- Could not resolve placeholder in string value
- Is spring default scope singleton or not?
- Java can’t find file when running through Eclipse
- How to solve the “failed to lazily initialize a collection of role” Hibernate exception
- MultipartException: Current request is not a multipart request
- Invalid escape sequence (valid ones are \b \t \n \f \r \” \’ \\ )
- JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object
- Java FileOutputStream Create File if not exists
- Configure active profile in SpringBoot via Maven
- How to read file from relative path in Java project? java.io.File cannot find the path specified
- How can I get the count of line in a file in an efficient way?
- java.lang.IllegalStateException: Failed to introspect Class
- Spring boot: Unable to start embedded Tomcat servlet container
- when spring boot startup,throw out the “method names must be tokens” exception
- model.addAttribute() parameters
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- In Java, how do I parse XML as a String instead of a file?
- “int cannot be dereferenced” in Java
- What is a Key-Value Pair?
- Create ArrayList from array
- What is null in Java?
- MacOS Catalina(v 10.15.3): Error: “chromedriver” cannot be opened because the developer cannot be verified. Unable to launch the chrome browser
- Can I catch multiple Java exceptions in the same catch clause?
- What is the use of printStackTrace() method in Java?
- How do I limit the number of decimals printed for a double?
- Please initialize the log4j system properly warning
- Can an abstract class have a constructor?
- org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
- Recursive print Factorial
- Recompile with -Xlint:unchecked for detail error in java
- Convert array of strings into a string in Java
- What is Serialization?
- SSL and cert keystore
- How to get locator of current web element?
- javax.naming.NameNotFoundException
- How to print out all the elements of a List in Java?
- If Java is Strongly typed then why does this code compile?
- Error: cannot find symbol ArrayList
- Choosing the best concurrency list in Java
- Why nextLine() and not nextString() ?
- Very Basic Java
- accessing a variable from another class
- “Javac” doesn’t work correctly on Windows 10
- How to convert a byte array to a hex string in Java?
- How to get the last value of an ArrayList
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- How to check if the value is integer in java?
- .class vs .java
- Case expressions must be constant expressions for static final int?
- try catch ArrayIndexOutOfBoundsException?
- Response has aready been committed
- How do I test a class that has private methods, fields or inner classes?
- how to make jni.h be found?
- difference between System.out.println() and System.err.println()
- Selenium — How to wait until page is completely loaded
- java.lang.IllegalThreadStateException
- What is an
method in Java? Can it be overridden? - The sum of all squares between 1 and 100 inclusive?
- How do I initialize a byte array in Java?
- How to map a composite key with JPA and Hibernate?
- Javac “cannot find symbol”
- Any shortcut to initialize all array elements to zero?
- Simple HTTP server in Java using only Java SE API
- How to specify filepath in java?
- Issue installing Netbeans in Windows 10: Java SE Development Kit (JDK) was not found on this computer
- dequeue and enqueue methods in queue implementation
- How to specify the default error page in web.xml?
- Exception is never thrown in body of corresponding try statement
- Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from/to central (http://repo1.maven.org/maven2)
- Can’t seem to disable Java Automatic Update