java


How to get the HttpServletResponse response into Spring Boot’s REST controllers?


I am struggling to get HttpServletResponse response into Spring Boot’s REST controller. The purpose behind is that I would like to return a file stream from a REST controller.
Here is the code.
#Component
#Api(value = "/api/1/download", description = "act upon selected file.")
#Path("/api/1/download")
#Consumes(MediaType.APPLICATION_JSON)
#RestController
public class DownloadResource {
private final Logger log = LoggerFactory.getLogger(DownloadResource.class);
#Autowired
HttpServletResponse response;
#ApiOperation(value = "Download a selected file", notes = "allows to download a selected file")
#Path("/downloadFile")
#POST
#Autowired
public void download(Object fileObject) {
String name = (String) ((LinkedHashMap) fileObject).get("path");
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "attachment; filename=abcd.txt");
try
{
Files.copy(Paths.get(name), response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
Neither File is download nor it throwing an error. Please help suggest. Thank you.
Give this a try. Did I understand what you were trying to do?
#SpringBootApplication
#RestController
public class FiledownloadApplication {
public static void main(String[] args) {
SpringApplication.run(FiledownloadApplication.class, args);
}
#PostMapping("/downloadFile")
public ResponseEntity<FileSystemResource> download(#RequestBody FileDownload fileDownload) throws IOException {
String path = fileDownload.getPath();
FileSystemResource fileSystemResource = new FileSystemResource(path);
return new ResponseEntity<>(fileSystemResource, HttpStatus.OK);
}
class FileDownload {
String path;
public FileDownload() {
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
}

Related Links

How to extract the boolean value of the branch decision?
Spring Boot findByIdIn internal list
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')' when using cast or convert function
Java WireMock automated testing with generated files
How to make JavaFX text wrap work?
Change sysout to String
jackson: (de)serialization multiple different ways
How to sync directory with AWS S3 using Java SDK?
Populating TableColumn CellValueFactory with List<Integer>
Is it Possible to Convert Existing Java Projects into Spring-Batch Jobs
How to insert an index of an array to another array through conditions in java
Is Jetty websocket client class WebSocketClient thread safe?
Spring #Async with rate per second
JPanel are not repainting in mouseClicked method
how does Add Headset button support to your Android application?
Android Studio: No view found for id (WebView fragment project) [duplicate]

Categories

HOME
pandas
ionic-framework
variables
date
zeromq
oracle11g
comparison
jpeg
umd
in-app-purchase
row
postgres-xl
timeout
ups
dynamics-crm-online
progressive-web-apps
saxon
alpine
javacv
tdd
buck
facebook-apps
jaxb2
iframe-resizer
scorm2004
mozilla
main
qwerty
abstract-class
ssjs
turbogears
logfiles
azure-application-gateway
jxcore
crypt
capacity
spring-cache
vtigercrm
pintos
setuptools
savon
ctest
storekit
pagedlist
blackberry-10
master-slave
dataview
php-internals
intellij-14
emailrelay
arcanist
cisco-ios
unity3d-gui
sortedlist
umbraco6
modalpopup
google-style-guide
asp.net-web-api-odata
android-radiobutton
funcunit
doskey
dealloc
picturefill
datagridviewcolumn
monomac
intentservice
industrial
ivyde
back-stack
wsdl-2.0
sublist
qtkit
bubble-chart
nsmanagedobject
reddot
mod-auth
propagation
jmock
lpeg
visitor-statistic
yslow
wise
mtj
project-hosting
zend-decorators
grid-system
document-conversion

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile