练手之作-公司点饭系统

原创 2015年07月08日 13:52:18

作为一个开发新手,公司的项目一直都帮不了大忙,所以就兼职了部门的内勤,每天统计订饭人数,别看是个小事情,但是每次都很麻烦,问来问去最后不是剩下就是不够,所以就加班写了一个小系统,内部点饭系统,现总结一下,开发过程中要感谢我的”同桌”和其他的同事,给我解答了一些问题。

1.使用poi生成Excel文件的时候,单元格强制换行:拼接字符串的时候要加上”\r\n”,注意斜杠不要写反了。

2.用流的方式下载Excel文件的时候如果是中文的文件名一定要编码,不然文件是可以下载的,但是命名很不好。
输出流的时候reponse要加上这两句代码

response.setContentType("application/octetstream,charset=utf-8");
response.addHeader("ContentDisposition","attachment;fileName=setmeal.xls");

我的下载Excel文件代码段

 String path=request.getSession().getServletContext().getRealPath("/")+"excel"+File.separator;
        String filepath=path+"setmeal.xls";
        exportExcel.outputExcel(filepath,request);//这句是自己写的excel导出的方法

        try {
             //filepath是指欲下载的文件的路径。 
            File file = new File(filepath);        
            response.setContentType("application/octet-stream,charset=utf-8");
            response.addHeader("Content-Disposition", "attachment;fileName=setmeal.xls");

            BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
            byte[] buf = new byte[1024];
            int len = 0;
                OutputStream out = response.getOutputStream();
                while ((len = br.read(buf)) > 0)
                    out.write(buf, 0, len);
                    br.close();
                    out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

3.用到了session,web.xml里面配置的session的过期时间为5分钟

    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>

4.用到了图片上传,因为要上传小票图片,用了公司项目里的压缩图片方法
压缩图片代码段

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import com.sun.jimi.core.Jimi;
import com.sun.jimi.core.JimiException;
import com.sun.jimi.core.JimiWriter;
import com.sun.jimi.core.options.JPGOptions;
import com.twl.web.util.image.AnimatedGifEncoder;


public class ImageUtil {

    public static void toJPG(String source, String dest, int quality) {
        if (dest == null || dest.trim().equals(""))
            dest = source;

        if (!dest.toLowerCase().trim().endsWith("jpg")) {
            dest += ".jpg";
            System.out.println("Overriding to JPG, output file: " + dest);
        }
        if (quality < 0 || quality > 100 || (quality + "") == null
                || (quality + "").equals("")) {
            quality = 100;
        }
        try {
            JPGOptions options = new JPGOptions();
            options.setQuality(quality);
            ImageProducer image = Jimi.getImageProducer(source);
            JimiWriter writer = Jimi.createJimiWriter(dest);
            writer.setSource(image);

            writer.setOptions(options);

            writer.putImage(dest);
        } catch (JimiException je) {
            System.err.println("Error: " + je);
        }
    }

    // 2.图片压缩(修改图片长和宽)
    public static void resize(String source, String desc, int width, int height) {
        try {
            // 读入文件
            BufferedImage src = ImageIO.read(new File(source));
            Image image = src.getScaledInstance(width, height,
                    Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_3BYTE_BGR);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            ImageIO.write(tag, "JPEG", new File(desc));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 2.图片压缩(修改图片长和宽)
    public static void resize(String source, OutputStream output, int width, int height) {
        try {
            // 读入文件
            BufferedImage src = ImageIO.read(new File(source));
            Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            ImageIO.write(tag, "JPEG", output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 3.图片裁剪
    public static void cut(String source, String desc, int x, int y, int width,
            int height) {
        try {

            FileInputStream is = new FileInputStream(source);

            Iterator<ImageReader> it = ImageIO
                    .getImageReadersByFormatName("jpg");
            ImageReader reader = it.next();
            ImageInputStream iis = ImageIO.createImageInputStream(is);

            reader.setInput(iis, true);

            ImageReadParam param = reader.getDefaultReadParam();

            Rectangle rect = new Rectangle(x, y, width, height);

            param.setSourceRegion(rect);

            BufferedImage bi = reader.read(0, param);

            ImageIO.write(bi, "jpg", new File(desc));
        } catch (FileNotFoundException e) {
            System.out.println("Can't find the image file: " + source);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Unable to get the input stream :" + source);
            e.printStackTrace();
        }
    }

    /**
     * @param source
     * @param dest
     * @throws JimiException
     */
    public static void toGIF(String source, String dest) throws JimiException {
        if (dest == null || dest.trim().equals(""))
            dest = source;
        // 1:转换为jpg
        if (!dest.toLowerCase().trim().endsWith("jpg")) {
            dest += ".jpg";
        }
        toJPG(source, dest, 75);

        BufferedImage file_in = null;
        File file = new File(dest);
        try {
            file_in = javax.imageio.ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int end = dest.lastIndexOf(".");
        file.deleteOnExit();
        // output *.gif
        file.renameTo(new File(dest.substring(0, end) + ".gif"));
        // jpg to gif
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.start(dest);
        e.addFrame(file_in);
        e.finish();

        /*
         * //分解GIF: GifDecoder d = new GifDecoder(); d.read("sample.gif"); int n
         * = d.getFrameCount(); for(int i = 0; i < n; i++) { BufferedImage frame
         * = d.getFrame(i); // frame i int t = d.getDelay(i);
         * // display duration of frame in milliseconds
         * // do something with frame }
         * 
         * //合成GIF: AnimatedGifEncoder e = new AnimatedGifEncoder();
         * e.start(outputFileName); e.setDelay(1000); // 1 frame per sec
         * e.addFrame(image1); e.addFrame(image2); e.finish();
         */
    }

    /**
     * @param img
     * @param dest
     * @throws JimiException
     */
    public static void toTIF(Image img, String dest) throws JimiException {
        if (!dest.toLowerCase().trim().endsWith("tif")) {
            dest += ".tif";
            System.out.println("Overriding to TIF, output file: " + dest);
        }
        dest = dest.substring(0, dest.lastIndexOf(".")) + ".jpg";
        try {
            System.out.println("toTIF encode");
            JimiWriter writer = Jimi.createJimiWriter(dest);
            writer.setSource(img);
            dest = dest.substring(0, dest.lastIndexOf(".")) + ".tif";
            writer.putImage(dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 线性改变图片尺寸(可同时改变图片格式)
     * 
     * @param source
     *            源文件完整路径
     * @param desc
     *            目标文件完整路径
     * @param ins
     *            放大倍数(小于1则为缩小)
     * @throws JimiException
     * @throws IOException
     */
    public static void changeDimension(String source, String desc, double ins)
            throws JimiException, IOException {
        String temp = desc;
        File _file = null;
        if (desc == null || desc.trim().equals(""))
            desc = source;
        if (!desc.toLowerCase().trim().endsWith("jpg")) {
            temp = desc.trim() + ".jpg";
        }
        toJPG(source, temp, 75);
        _file = new File(temp); // 读入文件

        Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
        double wideth = (double) src.getWidth(null); // 得到源图宽
        double height = (double) src.getHeight(null); // 得到源图高
        System.out.println("源图宽:" + wideth);
        System.out.println("源图高:" + height);

         //缩放处理
        int iWideth = (int) (wideth * ins); 
        int iHeight = (int) (height * ins);

        System.out.println("现图宽:" + iWideth);
        System.out.println("现图高:" + iHeight);
        System.out.println("缩放参数:" + ins);
        BufferedImage tag = null;
        try {
            tag = new BufferedImage(iWideth, iHeight,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
            /*
             * //打水印 File _filebiao = new File("test2.jpg"); Image src_biao =
             * ImageIO.read(_filebiao); int wideth_biao =
             * 40;//src_biao.getWidth(null); int height_biao =
             * 40;//src_biao.getHeight(null);
             * tag.getGraphics().drawImage(src_biao
             * ,50,50,wideth_biao,height_biao,null);
             */
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!temp.trim().equals(desc))
            _file.deleteOnExit();

        if (desc.toLowerCase().trim().endsWith("gif")) {
            System.out.println("the target type is gif!");
            AnimatedGifEncoder e = new AnimatedGifEncoder();
            e.start(desc);
            e.addFrame(tag);
            e.finish();
        } else if (desc.toLowerCase().trim().endsWith("tif")
                || desc.toLowerCase().trim().endsWith("tiff")) {
            System.out.println("the type is tif!");
            toTIF(tag, desc);
        } else {
            try {
                System.out.println("common type!");
                JimiWriter writer = Jimi.createJimiWriter(desc);
                writer.setSource(tag);
                writer.putImage(desc);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void transpic(String infile, int iWideth, int iHeight,
            String type) throws JimiException, IOException {
        String temp = infile;
        File _file = null;
        /*
         * if (outfile == null || outfile.trim().equals("")) outfile = infile;
         * if (!outfile.toLowerCase().trim().endsWith("jpg")) { temp =
         * outfile.trim() + ".jpg"; }
         */
        int index = infile.indexOf('.');
        if (index != -1 && "jpg".equals(infile.substring(index))) {
            toJPG(infile, temp, 75);
        }
        _file = new File(temp); // 读入文件

        Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象
        double wideth = (double) src.getWidth(null); // 得到源图宽
        double height = (double) src.getHeight(null); // 得到源图长
        System.out.println("源图宽:" + wideth);
        System.out.println("源图长:" + height);
        /*
         * //缩放处理 int iWideth = (int) (wideth * ins); int iHeight = (int)
         * (height * ins);
         */
        // int iWideth = 300;
        // int iHeight = 200;
        System.out.println("现图宽:" + iWideth);
        System.out.println("现图长:" + iHeight);
        String outfile = "";
        BufferedImage tag = null;
        try {
            tag = new BufferedImage(iWideth, iHeight,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图
            /*
             * //打水印 File _filebiao = new File("test2.jpg"); Image src_biao =
             * ImageIO.read(_filebiao); int wideth_biao =
             * 40;//src_biao.getWidth(null); int height_biao =
             * 40;//src_biao.getHeight(null);
             * tag.getGraphics().drawImage(src_biao
             * ,50,50,wideth_biao,height_biao,null);
             */
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!temp.trim().equals(outfile))
            _file.deleteOnExit();

        if (outfile.toLowerCase().trim().endsWith("gif")) {
            System.out.println("the type is gif!");
            AnimatedGifEncoder e = new AnimatedGifEncoder();
            e.start(outfile);
            e.addFrame(tag);
            e.finish();
        } else if (outfile.toLowerCase().trim().endsWith("tif")
                || outfile.toLowerCase().trim().endsWith("tiff")) {
            System.out.println("the type is tif!");
            toTIF(tag, outfile);
        } else {
            try {
                System.out.println("common type!");
                JimiWriter writer = Jimi.createJimiWriter(outfile);
                writer.setSource(tag);
                writer.putImage(outfile);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String dest = "E:\\desc.png";
        String source = "E:\\1431945665044.JPG";
//      int quality = 100;
        resize(source, dest, 256, 168);
    }

}

我的图片上传直接放在项目下,没有用图片服务器。

练手之作,一个简单的计算器

  • 2015年09月12日 17:15
  • 3.49MB
  • 下载

[python]基于BeautifulSoup解析百度贴吧water_maker(谁最水)的脚本,练手打趣之作[更新0.3]

5.29 百度在第二页添加了一个标题,导致扫的时候又出错了,原来维护这样一个小东西也是不容易的啊 代码更新 5.22修复不可抗拒BUG 这是一个不知道怎么回事,发生在BeautifulSoup...
  • whu_ksp
  • whu_ksp
  • 2012年05月13日 10:36
  • 2411

公司小项目,值得新人练手

  • 2015年05月31日 16:58
  • 50.87MB
  • 下载

天意之作--汉澳现代战争军事指挥作战系统 v2.1 terminator

天意之作--汉澳现代战争军事指挥作战系统 v2.1  terminator 前所未有的作战理论,极度简单的胜战秘诀,精确量化的系统工程,移动碉堡的新锐武器,闻所未闻的战斗部署,骇人听闻的核战反击,只有...

46家公司的笔试题,拿去练练手吧

原版是在微盘推荐里看到的,也不清楚是哪位有心人整理的,我先贴出来给大家瞧瞧,也替大家谢谢这位有心人!!欢迎一起讨论!话不多说,直接开始了: Sony 笔试题 1.完成下列程序 * *.*.  *....

天意之作--汉澳现代战争军事指挥作战系统 v2.0

天意之作--汉澳现代战争军事指挥作战系统 v2.0 前所未有的作战理论,极度简单的胜战秘诀,精确量化的系统工程,移动碉堡的新锐武器,闻所未闻的战斗部署,骇人听闻的核战反击,只有想不到,没有做不到。构成...

网上考试系统(吾师之作共享之)

  • 2008年12月09日 19:35
  • 4.61MB
  • 下载

QSplat:基于点绘制的图形学开山之作

QSplat简介 QSplat——基于点绘制的图形学开山之作:2000年随着Rusinkiewicz等人的Qsplat绘制系统出现, 点绘制技术有了重要突破。Rusinkiewicz和Levoy给Qs...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:练手之作-公司点饭系统
举报原因:
原因补充:

(最多只允许输入30个字)