| | |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.apache.commons.lang.time.DateUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * 文件上传接口 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * PDF文件预览 |
| | | * @param response |
| | | */ |
| | | @RequestMapping("/show-pdf") |
| | | public void showFile(String fileTime, String fileName, String filePathType, HttpServletResponse response) throws Exception { |
| | | |
| | | if (!fileName.endsWith(".pdf")) { |
| | | return; |
| | | } |
| | | Date time = DateUtils.parseDate(fileTime, new String[]{"yy-MM-dd HH:mm:ss"}); |
| | | |
| | | String filePath = fileManager.getPathByType(filePathType, time); |
| | | if(StringUtils.isEmpty(filePath)){ |
| | | return; |
| | | } |
| | | |
| | | File file = new File(filePath, fileName); |
| | | try { |
| | | response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + URLEncoder.encode(file.getName(), "UTF-8")); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | ServletOutputStream os = null; |
| | | try { |
| | | os = response.getOutputStream(); |
| | | os.write(FileUtils.readFileToByteArray(file)); |
| | | os.flush(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (os != null) { |
| | | try { |
| | | os.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |