读文件到网页上

package com.example.copydemo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

/**
 * @program: copy-demo
 * @description:
 * @author: Gaojq
 * @create: 2020-01-14 16:29
 **/

@RestController
@RequestMapping("file")
public class CopyController {

    protected static final Logger logger= LoggerFactory.getLogger(CopyController.class);

    /**
     * 网页上直接查看文件内容
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("look")
    public String look() throws FileNotFoundException, UnsupportedEncodingException {
        FileInputStream fis=new FileInputStream("D:\\git\\trade\\code\\trade_sync\\src\\main\\java\\com\\newflows\\sync\\executor\\ExecutorConfig.java");//选定文件
        InputStreamReader isr=new InputStreamReader(fis,"utf-8");//读入文件
        BufferedReader br=new BufferedReader(isr);//将读入的文件放入缓冲区
        String line;
        String tmp ="<pre style='background: #eee;  padding: 10px; border-radius: 5px;'>";
        while(true)//在缓冲区中不断取出一行数据
        {
            try {
                if (!((line=br.readLine())!=null)) {
                    break;
                }
                System.out.println(line);
                tmp+=line;
                tmp+="\n";
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        tmp+="</pre>";
        return tmp;
    }
}

http://localhost:8080/file/look

posted @ 2020-01-14 18:30  An-Optimistic-Person  阅读(170)  评论(0编辑  收藏  举报