I'm currently implementing the ability to download uploaded files.
The function works well, The "Save As" window does not appear.
I will attach the code that performs the download function.
DownLoadView.java
public class DownLoadView extends AbstractView
{
private File file;
public DownLoadView(File file)
{
setContentType("application/octet-stream");
this.file = file;
}
@Override
protected void renderMergedOutputModel(Map<String, Object> arg0, HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
resp.setContentType(getContentType());
resp.setContentLength((int) file.length());
System.out.println("getContentType >> " + resp.getContentType());
String userAgent = req.getHeader("User-Agent");
boolean ie = userAgent.indexOf("MSIE") > -1;
String fileName = file.getName();
if(ie)
{
fileName = URLEncoder.encode(file.getName(), "utf-8").replaceAll("\\+", "%20");
}
else
{
fileName = new String(file.getName().getBytes("utf-8")).replaceAll("\\+", "%20");
}
resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream out = resp.getOutputStream();
FileInputStream fis = null;
System.out.println("resp : " + resp);
try
{
fis = new FileInputStream(file);
FileCopyUtils.copy(fis, out);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(fis != null)
{
try
{
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
out.flush();
}
}
I studied MIME-TYPE and Content-Type to implement the file download function.
As a result, as far as I can tell, To perform the "Save As" function,
resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
I found that you need to set the "Content-Disposition".
I set it as it is, but the "Save As" window does not appear. (When I open your browser with Chrome)
As a result of taking a log of ContentType,
getContentType >> application/octet-stream;charset=UTF-8
I confirmed that it is set like the above log.
I set something wrong and the "Save As" window does not pop up?
I would appreciate it if you could tell me what went wrong.
Oh, and one more question.
To test these things, I tried to download files from Microsoft Edge browser and firefox browser.
For Edge, the "Save As" window opens!
And for fire fox, the "Save As" window does not appear. However, a check window is opened for whether to open or save the file.
Is this because of the properties that each browser has?
- Why does not the "Save As" window appear on my logic?
- Why do I get a file download window for each browser type when I download a file?