TW实习日记:第29-30天

  这两天挺忙,赶工期,改bug。项目现场的同事说客户火大得不行。可是谁叫你们谈工期谈的这么紧,完全不考虑开发的情况,真的是烦人这种事情。这两天遇到的最有难度的一个点就是附件预览,搞这个改到晚上11点。

  其实这个功能其实也不难,按理说还特别简单,因为只需要返回前端一个文件的URL地址就行了。但是呢,项目的网端服务器上的文件,前端通过地址不知道为什么无法访问,显示没有访问权限,同事说是跨域的问题。具体我也不了解这些偏前端的东西,所以没办法,就只能通过我自己写的后台向文件的URL做一些处理。通过同事的一些讲解,我知道了如果要让浏览器打开文件而不是下载的话,就需要改响应头的一些参数。

response.setContentType("application/pdf");//这个要根据文件的后缀名来动态判断,这是告诉浏览器这个页面的内容是什么类型,需要怎么解读
response.setHeader("Content-Disposition","inline");//这个参数就是告诉浏览器这个文件是预览形式,而不是点击后下载

  主要的参数就是这两个,通过这两个来达到预览附件的效果。贴一下我完整的处理预览附件的代码:

  

    public void openFile(){
        String u = request.getParameter("url");
        BufferedInputStream in = null;
        ByteArrayOutputStream out = null;
        //首先我采用的方式是使用字节流的方式,读取文件的字节流并保存,然后再将文件的字节流全部写入到响应中,直接输出在浏览器上,达到预览附件的效果
        try{
            u = URLDecoder.decode(u,"utf-8");
            URL url = new URL(u);
            in = new BufferedInputStream(url.openStream());
            out = new ByteArrayOutputStream(1024);
            byte[] temp = new byte[1024];
            int size = 0;
            while ((size = in.read(temp)) != -1) {
                out.write(temp, 0, size);
            }
            in.close();
            byte[] content = out.toByteArray();
            File file = new File(u);
            String fileName = file.getName();
            //动态处理响应头的contentType参数,不过这里我写的判断语句非常啰嗦,如果有更好的形式可以告诉我,谢谢
            if(file.getName().lastIndexOf(".") > 0){
                String extention = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

                if ("txt".equalsIgnoreCase(extention)) {
                    response.setContentType("text/plain;charset=gbk");
                } else if ("pdf".equalsIgnoreCase(extention)) {
                    response.setContentType("application/pdf");
                } else if ("doc".equalsIgnoreCase(extention)) {
                    response.setContentType("application/msword");
                } else if ("docx".equalsIgnoreCase(extention)) {
                    response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                } else if ("xls".equalsIgnoreCase(extention)) {
                    response.setContentType("application/vnd.ms-excel");
                } else if ("xlsx".equalsIgnoreCase(extention)) {
                    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                } else if ("jpg".equalsIgnoreCase(extention) || "jpeg".equalsIgnoreCase(extention)) {
                    response.setContentType("image/jpeg");
                } else if ("png".equalsIgnoreCase(extention)) {
                    response.setContentType("image/png");
                } else if ("gif".equalsIgnoreCase(extention)) {
                    response.setContentType("image/gif");
                } else if ("htm".equalsIgnoreCase(extention) || "html".equalsIgnoreCase(extention)) {
                    response.setContentType("text/html;charset=gbk");
                }
            }
            response.setHeader("Content-Disposition","inline");
            response.getOutputStream().write(content);

        }catch (Exception e){
            e.printStackTrace();
        }
    }                                                                   

  最后后台代码其实写的很快,当天晚上写好后台之后,却一直没有效果。我也不知道哪里出了问题,在项目中就是没有效果。我部署了n次却一点改变都没有...结果最后用safari打开,却发现地址都不是我修改过后的地址。然后就让我想到了该不会我的手机浏览器缓存了我之前的JS脚本代码把,所以我就在微信浏览器中刷新了一下页面,然后就成功了...找这个原因找了我可能有一个多小时,这个惨痛的教训告诉我以后做前端的东西要有清除缓存的好习惯...

  挖坑清单:

  1. Vue缓存机制、生命周期和钩子函数
  2. git学习与常用命令记录(最后的整理)
  3. 和czh开发练习博客demo(建表完成度1/3)
  4. 学习Java多线程基础
  5. 学习Java网络编程

 

Terence Xie

2018.8.25 周六 14:20

posted @ 2018-08-25 14:21  水猿  阅读(268)  评论(0编辑  收藏  举报