因为客户要预览txt,但是window.open打开路径是乱码,

然后又尝试js根据url创建File对象,找了半天没有找到,

搜索结果都是需要获取input 的file,但是input file为了安全不允许赋值,这特么难道让我选服务器里的文件????

很明显我做不到,最后只能使用最笨的方法了

那就用java自带的吧,由于牵扯换行的问题我就使用了readLine,没有换行的小伙伴可以使用BufferedInputStream方式根据自定义大小读取

BufferedInputStream bufferedInput = new BufferedInputStream(connection.getInputStream());
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
    String chunk = new String(buffer, 0, bytesRead);
}

 

 StringBuffer strB = new StringBuffer();   //strB用来存储txt文件里的内容
    String str = "";
    String mediaUrl = request.getAttribute("mediaUrl").toString();//文件路径我是从后台传的 就是 http://127.0.0.1......
    URL url = null;
    try {
        url = new URL(mediaUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        String txtSource = "";
        BufferedReader br = new BufferedReader(isr);
        while ((str = br.readLine()) != null) {
            strB.append(str).append("<br>");   //将读取的内容放入strB
        }
        br.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }