图片上传之二

String imgObj = request.getParameter(fileNodeName);
            Base64 base64 = new Base64();
            if (imgObj != null && !imgObj.equals("")) {

                if (imgObj.contains("jpeg")) {
                    filename = "jpeg";
                    imgObj = imgObj.replace("data:image/jpeg;base64,", "");
                } else if(imgObj.contains("jpg")){
                    filename = "jpg";
                    imgObj = imgObj.replace("data:image/jpg;base64,", "");
                }else if(imgObj.contains("png")){
                    filename = "png";
                    imgObj = imgObj.replace("data:image/png;base64,", "");
                }else{
                	throw new Exception();
                }

                byte[] bs = base64.decode(imgObj.getBytes());
                for (int i = 0; i < bs.length; ++i) {
                    if (bs[i] < 0) {// 调整异常数据
                        bs[i] += 256;
                    }
                }
                filename = datatime + "." + filename;
                path = releasePath + "/" + filename;
                LOGGER.debug(path + ":测试路径");
                OutputStream out = new FileOutputStream(path);
                InputStream is = new ByteArrayInputStream(bs);
                byte[] buff = new byte[1024];
                int len = 0;
                while ((len = is.read(buff)) != -1) {
                    out.write(buff, 0, len);
                }
                is.close();
                out.close();

                image_path = "/static/upload/images/admin/" + filename;

猜你喜欢

转载自blog.csdn.net/lv842586821/article/details/80419584