JavaWeb 网页上传文件

[外链图片转存中…(img-u4NVwWSZ-1596525867544)]
前端html

 <label class="car_lable">图片上传
                            <input type="file" style="margin-left: 83px" id="lcd_picture" name="lcd_picture" accept="image/*" placeholder="请上传图片" readonly  onchange="save_picture()">

                        </label>

前端ajax:

function save_picture() {
    var formData = new FormData();
    formData.append('lcd_picture',$("#lcd_picture")[0].files[0]);
    $.ajax({
        url:"LcdServlet/save_picture",
        type:"post",
        data:formData,
        dataType:"text",
        async: false,
        cache: false,
        processData: false,
        contentType: false,
        success:function (data) {
            switch (data){
                case "success":
                    send_success_alert();
                    append_picture();
                    break;
                case "fail":
                    alert("保存失败");
                    break;
                case "error":
                    alert("save_lcd_info-LedServlet/save_picture出错!");
                    break
            }
        }
    });
}

servlet:

public void save_picture(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");
        TxtToImg img = new TxtToImg();
        UrlMD5 um = new UrlMD5();
        Pictures pictures = new Pictures();
        PictureDao pictureDao = new PictureDao();

        String picture_md5 = null;                 //图片(md5)
        String pictureName = null;                 //图片文件名


        String path= null;
        try {
            //判断请求是否为multipar请求
            if (!ServletFileUpload.isMultipartContent(request)) {
                throw new RuntimeException("当前请求不支持文件上传");
            }
            //为基于磁盘的文件项创建一个FileItem工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //设置临时文件的边界值,大于该值时,上传文件会先保存在临时文件中,否则,上传文件将直接写入到内存
            //单位:字节,设置边界值100M,一字节=1024M;
            factory.setSizeThreshold(1024 * 1024 * 100);
            //设置文件临时储存
            String temppath = this.getServletContext().getRealPath("/temp");
            File temp = new File(temppath);
            factory.setRepository(temp);
            //创建一个新的文件上传处理程序
            ServletFileUpload upload = new ServletFileUpload(factory);
            //设置每一个item的头部字符编码,其可以解决文件名中文乱码问题;
            upload.setHeaderEncoding("UTF-8");
            //设置单个文件的最大边界值(这里是100M)
            upload.setFileSizeMax(1024 * 1024 * 100);
            //设置一次上传所有文件总和的最大值(对上传多个文件起作用,这里最大为100M)
            upload.setSizeMax(1024 * 1024 * 100);
            //得到一个保存了所有上传内容的List对象。
            List<FileItem> items = upload.parseRequest(request);
            //遍历
            for (FileItem item : items){
                //获取表单中属性名称
                String fieldName = item.getFieldName();    //getFieldName方法用于返回表单标签name属性的值
                switch (fieldName){
                    case "lcd_picture":
                       /*  path = "C:\\picture-up\\";*/
                        path = "/var/www/msp-lab.cn/bus/";
                        pictureName = img.uploadPicture(item,path);
                        Runtime.getRuntime().exec("chmod 644 /var/www/msp-lab.cn/bus/"+pictureName);
                        picture_md5 = um.md5(path+pictureName).toString();
                        break;
                }
            }
            pictures.setPic_name(pictureName);
            pictures.setPic_url("http://www.msp-lab.cn/bus/"+pictureName);
            pictures.setPic_md5(picture_md5);
            if (pictureDao.insert(pictures) > 0) {
                response.getWriter().write("success");//保存成功
            } else {
                response.getWriter().write("fail");//保存失败
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值