java Servlet 接收并保存TScrnCut截屏及扫描插件上传图像源码

使用Java Servlet保存 TScrnCut截屏插件及Web扫描插件上传的图像,如需保存为png请修改图像后缀,使用javascript设置插件返回图像格式

服务器端Servlet代码如下:

package TscrnCut;

import sun.misc.BASE64Decoder;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: nxiaoping
 * Date: 2014-1-10
 * Time: 9:36:23
 * To change this template use File | Settings | File Templates.
 */
public class uploadServlet  extends HttpServlet{

    public uploadServlet()
    {
        super();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doPost(request,  response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        String fileName = java.util.UUID.randomUUID().toString();
        try{
            String currentDirPath = getServletContext().getRealPath("/upfile/TScrnCut");
            byte[] fileDat = ImageStr2Bytes(request.getParameter("imgdat"));
            File currentDir = new File(currentDirPath);
            if(!currentDir.exists())
            {
                currentDir.mkdir();
            }
            file = new File(currentDirPath+"/"+fileName+".jpg");
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(fileDat);
        }catch(Exception e){

        }finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        response.getWriter().print("{src:'"+fileName+".jpg'}") ;
    }
    public static byte[] ImageStr2Bytes(String imgStr) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(imgStr);
            return b;
        } catch (Exception e) {

        }
        return null;
    }
}

插件调用、截图图像发送请访问以下链接 

浏览器截屏演示

http://d.iyanhong.com/demos/TScrnCut/sc2.html

Web扫描在线演示

http://d.iyanhong.com/demos/FileScan.aspx

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值