springboot 图片回显/文件回显

图片回显:




    /**
     * 图片回显
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/imageshow")
    public void imageShow(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("image/jpeg");
        OutputStream out = response.getOutputStream();
        String imgFileName = request.getParameter("myimage");
        String imgFileNameWithPath = filepathDir + "/images/" + imgFileName;
        try (FileImageInputStream input = new FileImageInputStream(new File(imgFileNameWithPath));
             ByteArrayOutputStream output = new ByteArrayOutputStream();) {
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = input.read(buf)) != -1) {
                output.write(buf, 0, len);
            }
            byte[] data = output.toByteArray();
            out.write(data);
            out.flush();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    

生成html文件:

  @PostMapping("save")
    public String saveArticle(@RequestParam("value") String value) {
        String filename = svaeFile(value);
        return filename;
    }


    public String svaeFile(String value) {

        StringBuilder sb = new StringBuilder();

        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("文章详情");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("");
        sb.append("
"); sb.append(value); sb.append("
"); sb.append(""); sb.append(""); sb.append(""); try { String uuid = UUID.randomUUID().toString(); String savePath = filepathDir + uuid + ".html"; File file = new File(savePath); if (!file.exists()) { file.createNewFile(); } FileOutputStream outputStream = new FileOutputStream(savePath); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); bufferedWriter.write(sb.toString()); bufferedWriter.newLine();// 换行 bufferedWriter.flush(); bufferedWriter.close(); return uuid + ".html"; } catch (IOException e) { return null; } }

显示文件:


/**
     * 显示文件
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/fileshow")
    public void fileShow(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        OutputStream out = response.getOutputStream();
        String imgFileName = request.getParameter("myfile");
        String imgFileNameWithPath = filepathDir + imgFileName;
        try (FileImageInputStream input = new FileImageInputStream(new File(imgFileNameWithPath));
             ByteArrayOutputStream output = new ByteArrayOutputStream();) {
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = input.read(buf)) != -1) {
                output.write(buf, 0, len);
            }
            byte[] data = output.toByteArray();
            out.write(data);
            out.flush();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }

你可能感兴趣的:(java,springboot,springboot)