I've created a download button to get a zip file of product photos. It works fine when testing in localhost but doesnt work after deployed to server.
No zip file was created and there're no error message in both web console and catalina log, nothing happened...
I once suspected it was due to file size issue. But spring boot seems to only have max upload size, not download. And the photos sizes are around 50KB to 150KB each, max 5 photos per zip file, so size shouldnt be the issue
Has anyone encountered similar issue before? Any suggestion would be appreciated!
frontend code (jquery) The reason that i didn't simply use window.open(link) is because the zip file name token my backend path (i.e. downloadAttachment.zip), so I did below as a work around
$("#download").click(function(){
var downloadLink = '<c:url value="/receiving/downloadAttachments.do"/>'
+"?productId=${productId}";
var linkElement = document.createElement("a");
linkElement.href = downloadLink;
linkElement.download = "product_image_${productId}.zip";
document.body.appendChild(linkElement);
linkElement.click();
document.body.removeChild(linkElement);
})
backend code (java) when I check the log, i realise the controller has not been called at all.
@RequestMapping(value="/downloadAttachments", method = RequestMethod.GET)
public void downloadAttachments( HttpServletRequest request, HttpServletResponse response,
@RequestParam(value="productId", required=true)Long productId) throws IOException{
log.debug("/downloadItemAttachments -- productId:"+productId);
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
List<ProductInfo> attachmentList = productInfoService.findByProductId(productId);
int contentLength = 0;
int seq = 1;
if(attachmentList!=null && !attachmentList.isEmpty()){
for(ProductInfo att : attachmentList){
String fileName = "item_"+productId+"_"+seq+".png";
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
//convert base 64 str to image
byte[] bytes = DatatypeConverter.parseBase64Binary(att.getBase64Str());
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
int length;
while ((length = bis.read(bytes)) >= 0) {
log.debug("file size : "+length);
contentLength += length;
zos.write(bytes, 0, length);
}
IOUtils.copy(bis, zos);
zos.closeEntry();
bis.close();
}
log.debug("contentLength -- "+contentLength);
zos.close();
String zipFileName = "product_image_"+productId+".zip";
response.setContentType("application/zip");
response.setStatus(HttpServletResponse.SC_OK);
response.setContentLength(contentLength);
response.setHeader("Content-Disposition","attachment;filename=\""+URLEncoder.encode(zipFileName,"UTF-8")+"\"");
response.flushBuffer();