Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.necl.core.controller; import com.necl.core.model.ConfigSystem; import com.necl.core.service.ConfigSystemService; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLConnection; import java.nio.charset.Charset; import java.util.Calendar; import java.util.Date; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * * @author C13.207 */ @Controller public class DownloadFileController { @Autowired ConfigSystemService configSystemService; @RequestMapping(value = "/download", method = RequestMethod.GET) public void downloadFile(HttpServletResponse response, @RequestParam String id) throws IOException, Exception { String pre_year = "20"; String post_year = id.substring(3, 5); String full_year = pre_year + post_year + "/"; String keyFind = "PATH"; ConfigSystem configSystem = configSystemService.findByKey(keyFind); String saveDirectory = configSystem.getConfigText() + full_year; File file = null; String name = id.replace("/", "_"); String type = ".pdf"; String pathDirectory = saveDirectory + name + type; System.out.println("path:" + pathDirectory); file = new File(pathDirectory); if (!file.exists()) { String errorMessage = "Sorry. The file you are looking for does not exist"; OutputStream outputStream = response.getOutputStream(); outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); outputStream.close(); return; } String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if (mimeType == null) { mimeType = "application/octet-stream"; } response.setContentType(mimeType); /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() + "\"")); /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int) file.length()); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); //Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream()); } @RequestMapping(value = "/downloadTraining", method = RequestMethod.GET) public void downloadFileTraining(HttpServletResponse response, @RequestParam String id) throws IOException, Exception { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); String keyFind = "PATH"; ConfigSystem configSystem = configSystemService.findByKey(keyFind); String saveDirectory = configSystem.getConfigText() + year + "/"; File file = null; String name = id.replace("/", "_"); String type = ".pdf"; String pathDirectory = saveDirectory + name + type; file = new File(pathDirectory); if (!file.exists()) { String errorMessage = "Sorry. The file you are looking for does not exist"; System.out.println(errorMessage); OutputStream outputStream = response.getOutputStream(); outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); outputStream.close(); return; } String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if (mimeType == null) { mimeType = "application/octet-stream"; } response.setContentType(mimeType); /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() + "\"")); /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int) file.length()); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); //Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream()); } @RequestMapping(value = "/downloadManual", method = RequestMethod.GET) public void downloadFileManual(HttpServletResponse response) throws IOException, Exception { String keyFind = "PATH"; ConfigSystem configSystem = configSystemService.findByKey(keyFind); String saveDirectory = configSystem.getConfigText() + "Manual/"; File file = null; String type = ".pdf"; String pathDirectory = saveDirectory + "ApprovalControlManual" + type; System.out.println("pthDi: " + pathDirectory); file = new File(pathDirectory); if (!file.exists()) { String errorMessage = "Sorry. The file you are looking for does not exist"; System.out.println(errorMessage); OutputStream outputStream = response.getOutputStream(); outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); outputStream.close(); return; } String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if (mimeType == null) { mimeType = "application/octet-stream"; } System.out.println("mimetype : " + mimeType); response.setContentType(mimeType); /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() + "\"")); /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int) file.length()); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); //Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream()); } @RequestMapping(value = "/downloadApprovalCriteria", method = RequestMethod.GET) public void downloadApprovalCriteria(HttpServletResponse response) throws IOException, Exception { String keyFind = "PATH"; ConfigSystem configSystem = configSystemService.findByKey(keyFind); String saveDirectory = configSystem.getConfigText() + "Manual/"; File file = null; String type = ".pdf"; String pathDirectory = saveDirectory + "ApprovalCriteria" + type; System.out.println("pthDi: " + pathDirectory); file = new File(pathDirectory); if (!file.exists()) { String errorMessage = "Sorry. The file you are looking for does not exist"; System.out.println(errorMessage); OutputStream outputStream = response.getOutputStream(); outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8"))); outputStream.close(); return; } String mimeType = URLConnection.guessContentTypeFromName(file.getName()); if (mimeType == null) { System.out.println("mimetype is not detectable, will take default"); mimeType = "application/octet-stream"; } System.out.println("mimetype : " + mimeType); response.setContentType(mimeType); /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/ response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() + "\"")); /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/ //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int) file.length()); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); //Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream()); } }