文件处理 下载

来源:互联网 发布:ios编程语言 编辑:程序博客网 时间:2024/06/08 02:07

感觉这一点需要记下来,记在别处又担心忘记了,所以新建一篇博客,给自己提个醒。

一、二维码的生成

public static void main(String[] args) {        //取当前时间为图片名称 带毫秒的        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");        Date d = new Date();        String str = sdf.format(d);        String imgPath = "F:/" + str + ".png";        String content = "要生成的信息";        QRCodeEncoderHandler handler = new QRCodeEncoderHandler();        handler.encoderQRCode(content, imgPath);        System.out.println("imgPath:" + imgPath);        System.out.println("encoder QRcode success");    }

public void encoderQRCode(String content, String imgPath) {        try {            Qrcode qrcodeHandler = new Qrcode();            // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小            qrcodeHandler.setQrcodeErrorCorrect('H');            qrcodeHandler.setQrcodeEncodeMode('B');            qrcodeHandler.setQrcodeVersion(5);            System.out.println(content);//           int imgSize = 67 + 12 * (size - 1);//            byte[] contentBytes = content.getBytes("gb2312");            byte[] contentBytes = content.getBytes("gbk");            BufferedImage bufImg = new BufferedImage(115, 115,                    BufferedImage.TYPE_INT_RGB);            Graphics2D gs = bufImg.createGraphics();            gs.setBackground(Color.WHITE);            gs.clearRect(0, 0, 115, 115);            // 设定图像颜色> BLACK            gs.setColor(Color.BLACK);            // 设置偏移量 不设置可能导致解析出错            int pixoff = 2;            // 输出内容> 二维码            if (contentBytes.length > 0 && contentBytes.length < 800) {                boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);                for (int i = 0; i < codeOut.length; i++) {                    for (int j = 0; j < codeOut.length; j++) {                        if (codeOut[j][i]) {                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);                        }                    }                }            } else {                System.err.println("QRCode content bytes length = "                        + contentBytes.length + " not in [ 0,120 ]. ");            }            gs.dispose();            bufImg.flush();            File imgFile = new File(imgPath);            // 生成二维码QRCode图片            ImageIO.write(bufImg, "png", imgFile);        } catch (Exception e) {            e.printStackTrace();        }    }
二、web程序中下载的实现。  需要对fileName进行处理,然后response的属性进行一些设置

这样就能打开浏览器的现在功能。否则就在浏览器显示你要下载的文件。对文件filename。

原理:暂时不明


//准备文件输出的名称String fileName ="barcode.ftl";//// 要填入ftl模板的数据文件SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式String wordname = "批次条码导出-"+df.format(new Date())+".doc";//文件名称try {   wordname=URLDecoder.decode(URLDecoder.decode(wordname, "UTF-8"), "UTF-8");   wordname = new String(wordname.getBytes(), "ISO-8859-1");   response.setHeader("Content-disposition", "attachment;filename="+ wordname);} catch (UnsupportedEncodingException e1) {   // TODO Auto-generated catch block   e1.printStackTrace();}response.setContentType("application/vnd.ms-excel;charset=utf-8");

参考链接:

http://www.jb51.net/article/85102.htm

http://takeme.iteye.com/blog/1683380

http://blog.csdn.net/oh_mourinho/article/details/7979003

http://jingyan.baidu.com/article/915fc4149e1f9a51394b2007.html

0 0
原创粉丝点击