Is the code below an efficient way to get the zip file from the server? The code works fine but I'm wondering if I should throw some exceptions for the following conditions:
if(param1 == null || param2 == null)
} else if ("".equals(param1) || "".equals(param2)) {
The way I call this servlet from a web browser is as follows:
https://myserver.com/DownloadFileFromServer/DownloadFileServlet?filename=my_users_files_1560866090.zip&user=TAN
Eventually, I'll call the above URL from my user interface when a user would have clicked a Download
button, maybe using Javascript's setTimeout() method. The reason I plan on calling it again and again for sometime is because the file might not be
available on the server and I would have to keep on checkiing it again and again. Do I need to handle this part in my servlet code somehow? I mean, I would keep on callling the above URL until I get the file
from the server.
@WebServlet("/DownloadFileServlet")
public class DownloadFileServlet extends HttpServlet {
final String[][] contentTypes = {{"zip" ,"application/zip"},{"csv" ,"text/csv"},{"pdf","application/pdf"}};
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param1 = request.getParameter("filename");
String param2 = request.getParameter("user");
System.out.println("File Name Retrieved is:"+param1);
System.out.println("User Name Retrieved is:"+param2);
final String FILE_LOCATION = "/srv/my_users/param2/";
if(param1 == null || param2 == null) {
// The Request Parameters Were Not Present In The Query String. Do Something Or Exception Handling !!
System.out.println("Request Parameter Not Found in first if!");
} else if ("".equals(param1) || "".equals(param2)) {
// The Request Parameters Were Present In The Query String But Has No Value. Do Something Or Exception Handling !!
System.out.println("Request Parameter Not Found in second if!");
} else {
/*if (param1 != null && param2 !=null) {*/
String fileName = (String) param1;
String contentType = getContentType(fileName.split("\\.")[1]);
File file = new File(FILE_LOCATION + "/" +fileName);
response.setContentType(contentType);
response.addHeader("Content-Disposition","attachment; filename=" + fileName);
response.setContentLength((int) file.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
int bytesRead = bufferedInputStream.read();
while (bytesRead != -1) {
servletOutputStream.write(bytesRead);
bytesRead = bufferedInputStream.read();
}
if (servletOutputStream != null) servletOutputStream.close();
if(bufferedInputStream != null) bufferedInputStream.close();
}
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
private String getContentType(String fileType) {
String returnType = null;
for(int i = 0; i < contentTypes.length; i++) {
if(fileType.equals(contentTypes[i][0])) returnType = contentTypes[i][1];
}
return returnType;
}
}