zoukankan      html  css  js  c++  java
  • 进入编辑页面,图片回显,图片下载

    需求简介:做编辑功能的时候,进入页面,要回显以前上传过的图片

    解决思路:点击编辑,后台查询需要编辑的信息(包含图片信息,其实就是图片的名字 xxx.jpg),然后把信息放到edit页面,进入编辑页面的时候,获取到该信息的图片名称,拼装下载路径,然后把该路径给img标签的src,页面加载src路径的图片时,就会进入到后台的下载方法,把图片从服务器上下载到本地,执行完后显示该图片。该方式是通过response那两行代码显示的,深入的不太懂,上代码

    代码:

    //js代码 获取后台穿过俩的plan对象,然后取得plan的图片名字plan.
    <script type="text/javascript">
    var plan = [[${plan}]];
    if (CommnUtil.notNull(plan)) {
    var planIcon = plan.planIcon;
    if (CommnUtil.notNull(planIcon)) {
    var imgPlanIcon = rootPath + 'serv/plan/downLoadImage?planIcon=' + planIcon;
         //给src赋值,使得当图片加载的时候进入后台的下载方法,执行完得到完整的图片路径,再显示图片
    $('#imgPlanIcon').attr('src', imgPlanIcon);
    }
    </script>
    //后台java代码
    @RequestMapping("downLoadImage")
    public void downLoadFile(String planIcon, HttpServletResponse response) {
    if (Tools.isEmpty(planIcon)) {
    return;
    }
    File file = null;
    file = new File(imagePath + File.separator + planIcon);
    if (!file.exists()) {
    file = new File(imageTempPath + File.separator + planIcon);
    }

    if (file.exists()) {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
    in = new BufferedInputStream(new FileInputStream(file));
    out = new BufferedOutputStream(response.getOutputStream());
    // 设置response内容的类型 https://blog.csdn.net/saytime/article/details/51497529第二种方法
    response.setContentType(new MimetypesFileTypeMap().getContentType(file));
    // 设置头部信息 Content-disposition指定的类型必须是文件的扩展名
    response.setHeader("Content-disposition", "attachment;filename=" + planIcon);
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = in.read(buffer)) > 0) {
    out.write(buffer, 0, length);
    }
    out.flush();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (in != null) {
    in.close();
    }
    if (out != null) {
    out.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    总结:其实流这一块儿挺薄弱的,一点一点的积累吧,另外,response我自己用到的真的不多,就是设置个编码集,或者获取write对象,也得总结总结

  • 相关阅读:
    PHPEXCEL 导出多个sheet
    android adb.exe端口占用
    imageview 显示assets的图片

    Java中日期问题
    java中的定时器的实现样例
    Oracle数据库如何干净的删除
    MySQL索引相关知识
    JavaScript基础知识总结
    JDBC技术总结
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/9816877.html
Copyright © 2011-2022 走看看