实现图片下载

package bootstarter;

import weaver.mvc.annotations.Controller;
import weaver.mvc.annotations.RequestMapping;
import weaver.mvc.mode.HttpMode;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;


// /mvc/accessory/getPictureByid?picID=241164
@Controller(URL = "accessory")
public class GetAccessory2 extends HttpServlet {

    @RequestMapping(URL = "getPictureByid")
    public void downloadPic(HttpMode mode, HttpServletResponse response){
        BufferedInputStream is = null;
        BufferedOutputStream os = null;
//        weaver.conn.RecordSet rs = new weaver.conn.RecordSet();
        String filename = "";// 文件名
        String filepath = "";// 文件路径
        try {
            String id = mode.getValue("picID");    //替换回来
            String picID = "241164";
            System.out.println("picID:" + id);
//            String fileSql = "select IMAGEFILENAME,FILEREALPATH from IMAGEFILE where IMAGEFILENAME like (select concat(docsubject,'%') from docdetail where id ="
//                    + picID + ") and IMAGEFILETYPE LIKE 'image%' and rownum = 1";
//            rs.executeSql(fileSql);
//            if (rs.next()) {
//                filename = weaver.general.Util.null2String(rs.getString("imagefilename"));
//                filepath = weaver.general.Util.null2String(rs.getString("filerealpath"));    //替换回来
//            }

            filename = "登入成功.png";
            filepath = "/weaver/ecology/filesystem/202208/Y/ad46e717-e18f-46e9-acc5-04803c8c5f80.zip";    //替换回来
            String savepath = "";
            int dex = filepath.lastIndexOf(".");    // 截取最后的一个“/”符号,获取长度索引
            if (dex > -1) {
                savepath = filepath.substring(0, dex) + ".";   // 获取到拼接的保存路径
            }
            int dex2 = filename.lastIndexOf(".")+1;
            String suffix = filename.substring(dex2, filename.length());

            filename = savepath + suffix;
            System.out.println("filename:" + filename);


            File file = new File(filename);   // 读取到文件
            response.setContentType("application/x-msdownload;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(file.getName()));
            response.setHeader("Content-Length", String.valueOf(file.length()));

            is = new BufferedInputStream(new FileInputStream(file));
            os = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = is.read(buff, 0, buff.length))) {
                os.write(buff, 0, bytesRead);
            }
            if (is != null)
                is.close();
            if (os != null)
                os.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值