I am facing the issue while downloading the csv file. I have created get api in spring boot and wanted to download the file through that api. Here is my code.
#GetMapping(value = "/citydetails/download")
public ResponseEntity<Object> getCityDetails() throws IOException {
System.out.println("Starting the rest call.");
FileWriter filewriter = null;
service = new LocalityService();
try {
List<CityDetails> details = service.getCityDetailsList();
if (details.isEmpty()) {
System.out.println("List is empty.");
}
StringBuilder fileContent = new StringBuilder("STATE,DISTRICT,CITY,VILLAGE,PIN\n");
for (CityDetails data : details)
fileContent.append(data.getStatename()).append(data.getDistrict()).append(data.getCity())
.append(data.getVillage()).append(data.getPindode());
XSSFWorkbook workbook = service.saveFileContents(details);
String filename = "C:\\Users\\" + System.getProperty("user.name") + "\\Downloads\\cityDetails.csv";
FileOutputStream out = new FileOutputStream(filename);
File file = new File("cityDetails.csv");
workbook.write(out);
out = new FileOutputStream(file);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("text/csv"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "cityDetails.csv" + "\"")
.body(file);
} catch (Exception ex) {
System.out.println("Failed to execute rest" + ex.getStackTrace() + "Locale: " + ex.getLocalizedMessage());
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), "false");
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
} finally {
if (filewriter != null)
filewriter.close();
}
}
Exception:
Here i have used XSSFWorkbook to save my content to csv. Now i wanted to download the csv file through Rest api which will contain the data.I have tried multiple ways but i am getting empty file.With the above code it will save the csv file in download folder in windows machine, but i want this api to work on every system so I wanted to download the csv file with contents instead of saving it to specific location. I am facing the issue, how to resolve that?
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet=wkb.createSheet("sheet1");
HSSFRow row1=sheet.createRow(0);
HSSFCell cell=row1.createCell(0);
cell.setCellValue("table_demo");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//input Excel date
HSSFRow row2=sheet.createRow(1);
row2.createCell(0).setCellValue("name");
row2.createCell(1).setCellValue("class");
row2.createCell(2).setCellValue("score_1");
row2.createCell(3).setCellValue("score_2");
HSSFRow row3=sheet.createRow(2);
row3.createCell(0).setCellValue("Jeams");
row3.createCell(1).setCellValue("High 3");
row3.createCell(2).setCellValue(87);
row3.createCell(3).setCellValue(78);
//output Excel file
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=details.xls");
response.setContentType("application/msexcel");
wkb.write(output);
output.close();
This code can get a simple excel file
//image
#GetMapping(value = "/image")
public #ResponseBody byte[] getImage() throws IOException {
InputStream in = getClass()
.getResourceAsStream("/com/baeldung/produceimage/image.jpg");
return IOUtils.toByteArray(in);
}
//normal file
#GetMapping(
value = "/get-file",
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
)
public #ResponseBody byte[] getFile() throws IOException {
InputStream in = getClass()
.getResourceAsStream("/com/baeldung/produceimage/data.txt");
return IOUtils.toByteArray(in);
}
Related
I have some error of open file.xlsx, genereted by librart Apache POI. File is save fine and open fine on my local machine. But after sending stream from java to vue it doesn't work. This is java:
public void sendFileToResponse(HttpServletResponse response, SearchParam searchParam) throws `IOException {`
String type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
String fileNameAttr = "filename=MyExcel.xlsx";
response.setHeader("Content-Disposition", "attachment;" + fileNameAttr);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setCharacterEncoding("utf-8");
response.setContentType(type + ";charset=UTF-8");
Workbook workbook;
FileOutputStream fileOutputStream = null;
ServletOutputStream outputStream = null;
try {
workbook = getFile(searchParam);
fileOutputStream = new FileOutputStream("MyExcel2.xlsx");
workbook.write(fileOutputStream);
outputStream = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
byte[] xlsx = baos.toByteArray();
outputStream.write(xlsx);
workbook.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
logger.error("Error: {}", e);
}
here i get it on frontend:
saveStatistic () {
client.get('/statistics/statisticUseOfData/download', {responseType: 'blob'})
.then(response => {
console.log(response)
this.downloadFileName = 'test.xlsx'
var aHref = document.createElement('a')
var binaryData = [];
binaryData.push(response.data);
var urlBlob = window.URL.createObjectURL(new Blob(binaryData, {type: "application/octet-stream"}))
aHref.href = urlBlob
aHref.download = this.downloadFileName
document.body.appendChild(aHref)
aHref.click()
window.URL.revokeObjectURL(urlBlob)
})
.catch(error => {
console.log(error)
})
and that part of response, which i have in console.log(response.data):
PKw1Q[Content_Types].xml�S�n�0����*6�PU�C���\{�X�%����]8�R�
q�cfgfW�d�q�ZCB|��|�*�*h㻆},^�{Va�^K<4�6�N�XQ�dž�9�!P��$��҆�d�c�D�j);��ѝP�g��E�M'O�ʕ����H7L�h���R���G��^�'�{��zސʮB��3�˙��h.�h�W�жF�j娄CQՠ똈���}ιL�U:D�����%އ����,�B���[� �� ;˱� �{N��~��X�p�ykOL��kN�V��ܿBZ~����q�� �ar��{O�PKz��q;PKw1Q_rels/.rels���j�0�_���8�`�Q��2�m��4[ILb��ږ���.[K
�($}��v?�I�Q.���uӂ�h���x>=��#��p�H"�~�}� �n����*"�H������8�Z�^'�#��7m{��O�3���G�u�ܓ�'��y|a�����D� ��l_EYȾ����vql3�ML�eh���*���\3�Y0���oJ� :��^��}PK��z��IPKw1QdocProps/app.xmlM��
You don't need to write the workbook into a file and then into memory, you can write directly into the response:
try {
workbook = getFile(searchParam);
workbook.write(response.getOutputStream());
} catch (Exception e) {
logger.error("Error: {}", e);
}
I found the problem. It was in one custom layer before axios, which override http requests. And someone didn't add any params for this get, just url
Thank's you
I am beginner in java and would like some assistance with zipping a downloaded file using rest api call to MSSQL backend. Below is the code snippet which takes the ID as input parameter, fetches the record specific for that ID and downloads it locally.
I now need the code modified to Zip the file when it is downloading.
#GetMapping("/message/save")
#CrossOrigin(origins = "*")
public ResponseEntity<byte[]> download(#RequestParam("id") Long id) throws Exception {
Optional<MessageEntity> messageRecord = messageRepository.findById(id);
MessageEntity messageEntity = messageRecord.get();
ObjectMapper objectMapper = new ObjectMapper();
String xml = objectMapper.writeValueAsString(messageEntity);
byte[] isr = xml.getBytes();
String fileName = "message.zip";
HttpHeaders respHeaders = new HttpHeaders();
respHeaders.setContentLength(isr.length);
respHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
respHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return new ResponseEntity<byte[]>(isr, respHeaders, HttpStatus.OK);
}
I expect the output to be a zipped file.
I'm not sure that I understood your problem clearly. But I assume that you need just make zip from string:
#GetMapping("/message/save")
#CrossOrigin(origins = "*")
public void download(#RequestParam("id") Long id, HttpServletRequest request,
HttpServletResponse response) throws Exception {
MessageEntity messageEntity = messageRepository.findById(id).orElseThrow(() -> new Exception("Not found!"));
String xml = new ObjectMapper().writeValueAsString(messageEntity);
String fileName = "message.zip";
String xml_name = "message.xml";
byte[] data = xml.getBytes();
byte[] bytes;
try (ByteOutputStream fout = new ByteOutputStream();
ZipOutputStream zout = new ZipOutputStream(fout)) {
zout.setLevel(1);
ZipEntry ze = new ZipEntry(xml_name);
ze.setSize(data.length);
zout.putNextEntry(ze);
zout.write(data);
zout.closeEntry();
bytes = fout.getBytes();
}
response.setContentType("application/zip");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition",
"attachment; "
+ String.format("filename*=" + StandardCharsets.UTF_8.name() + "''%s", fileName));
ServletOutputStream outputStream = response.getOutputStream();
FileCopyUtils.copy(bytes, outputStream);
outputStream.close();
}
We have web application with client in agnular.js and server in java spring. I am working on functionality of downloading this log file i.e. logs.tar from client.
Currently we are using blob to download. Our issue is in case this log size becomes huge like greater than 2GB then while streaming it will create load on application memory. so i want way to download large files chunk by chunk and not required to load entire blob into memory. please suggest way out.
Server side java code -
public ResponseEntity<?> downloadLogs(HttpServletRequest request) {
File file = preferencesService.downloadLogs();
if (file != null) {
FileInputStream inputStream;
try {
inputStream = new FileInputStream(file);
byte[] content = FileCopyUtils.copyToByteArray(inputStream);
String filename = "com-logs.tar";
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream"
);
return new ResponseEntity<byte[]>(content, responseHeaders, HttpStatus.OK);
} catch (Exception e) {
logger.error("Error while processing log file for download", e);
}
} else {
logger.error("Failed to download logs");
}
return ResponseEntity.badRequest().build();
}
Client side Angular.js code -
this._service.downloadLogs().subscribe(
success => {
var blb = new Blob([success], { 'type': "application/octet-stream" });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blb, 'logs.tar');
}
else {
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blb);
link.download = "logs.tar";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
});
New Server side java code -
public void downloadLogs(HttpServletResponse resonse) {
File file = preferencesService.downloadLogs(id);
if (file != null) {
try {
resonse.setContentType("application/octet-stream");
resonse.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
BufferedInputStream inStrem = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream outStream = new BufferedOutputStream(resonse.getOutputStream());
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inStrem.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.flush();
inStrem.close();
}
...
}
The important thing is to not read the file into memory, but to pass the stream on:
public ResponseEntity<?> downloadLogs(HttpServletRequest request) {
File file = preferencesService.downloadLogs();
if (file != null) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
InputStreamResource inputStreamResource =
new InputStreamResource(new inputStream);
HttpHeaders responseHeaders = new HttpHeaders();
//responseHeaders.setContentLength(Files.size(file.toPath()));
responseHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="
+ filename);
responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
return new ResponseEntity(inputStreamResource, responseHeaders, HttpStatus.OK);
}
}
...
}
Consider compression as this will hugely speed things up and cause less server load.
Chunking, setting content length, deflate compression web filters, and so on should be looked into.
I currently have an XSSFWorkbook and would like to cast it or somehow change it to File within the Java code. Is there any way of doing so?
Use XSSFWorkbook.write(java.io.OutputStream stream) to write the content to a file.
FileOutputStream out = new FileOutputStream("yourFileName.xls");
Workbook wb = new XSSFWorkbook();
//do your stuff ...
wb.write(out);
Bellow code is used and tested.
private Response sendExcelFile(Locale locale, Optional<List<List<String>>> consumersReportData) {
XSSFWorkbook workBook = ExportToExcelUtils.prepareWorkBook(consumersReportData.get(), "data");
String DisplayFileName = "Consumers-Report-" + DateUtils.getLocalDateInString(DateUtils.now());
String fileName = "/tmp/fileName.xlsx";
// Created file object here
File file = new File(fileName);
try {
FileOutputStream outputStream = new FileOutputStream(file);
workBook.write(outputStream);
} catch (FileNotFoundException e) {
LOGGER.error("File not found : fileName {} Exception details:{} ", fileName, e);
} catch (IOException e) {
LOGGER.error("IO exception : fileName {} Exception details:{} ", fileName, e);
}
ResponseBuilder responseBuilder = Response.ok((Object) file);
responseBuilder.header("Content-Disposition", "attachment; filename=" + DisplayFileName + EXCEL_FILE_EXTENSTION);
return responseBuilder.build();
}
I want to save zip archive from server to user computer. I have web page that shows some information about this file and has a download button. In my controller action on button simply redirect on homepage but I want to get data from database and save it to user machine with path is defined by user
The issue is that I don't know how I can get this path. Could you give me an example how I can do that?
In your controller method you can add this code to get file download
File file = new File("fileName");
FileInputStream in = new FileInputStream(file);
byte[] content = new byte[(int) file.length()];
in.read(content);
ServletContext sc = request.getSession().getServletContext();
String mimetype = sc.getMimeType(file.getName());
response.reset();
response.setContentType(mimetype);
response.setContentLength(content.length);
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
org.springframework.util.FileCopyUtils.copy(content, response.getOutputStream());
You don't have to know how to get the path, because the path is defined by the user :) But if your looking for the download path, check the source code of the website and where the download button links to. Usually you can see it in the beginning of the <form>.
If you are just looking for download a file:
public void download(String filename, String url) {
URL u;
InputStream is = null;
DataInputStream dis;
String s;
try{
u = new URL(url);
// throws an IOException
is = u.openStream();
dis = new DataInputStream(new BufferedInputStream(is));
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
while ((s = dis.readLine()) != null) {
// Create file
out.write(s);
//Close the output stream
out.close();
}
}catch (Exception e){ //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
is.close();
}
Hope this helps...
If you want to download from some external URL or from S3:::
#RequestMapping(value = "asset/{assetId}", method = RequestMethod.GET)
public final ResponseEntity<Map<String, String>> fetch(#PathVariable("id") final String id)
throws IOException {
String url = "<AWS-S3-URL>";
HttpHeaders headers = new HttpHeaders();
headers.set("Location", url);
Map<String, String> map = null;
ResponseEntity<Map<String, String>> rs =
new ResponseEntity<Map<String, String>>(map, headers, HttpStatus.MOVED_PERMANENTLY);
return rs;
}