vue+iview文件导入导出代码

博客围绕前端开发展开,介绍了使用Vue进行上传Base64图片的操作,后端将其以byte[]存入数据库的blob类型字段,还提及前端查看照片。此外,还讲解了用get方法请求下载导出的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、上传base64图片

upload:function () {
            var self = this;

            var uploads = self.uploadList;
            var names = self.fileNames;
            for (var i = 0; i < uploads.length; i++) {
                var upload = uploads[i];
                var name = names[i];
                jQuery.ajax({
                    type: "post",
                    url: baseUrl + 'xjgl/xjzpgl/plsczp',
                    data: {upload:upload,name:name,zplx:self.saveform.zplx},
                    async: true,
                    success: function (res) {
                        self.uploadList.splice(self.uploadList.indexOf(upload), 1)
                        self.fileNames.splice(self.fileNames.indexOf(name), 1)
                        self.$Notice.success({
                            title: '文件上传成功',
                            desc: '文件 ' + name + ' 上传成功。'
                        });
                    }
                })
            }

        },

后端 存入的是byte[] 存进数据库的blob类型的字段中

@ResponseBody
    @RequestMapping(value = "plsczp" , method = RequestMethod.POST )
    public String plsczp(String upload,String name,XjzpPage xjzpPage,@AuthenticationPrincipal  UserDetails user,HttpServletRequest request){
        String username = user.getUsername();
        try {
            String[] filenames = name.split("\\.");
            String fileType = filenames[filenames.length-1];
            String filename = name.substring(0,name.indexOf(fileType)-1);

            long timestamp=System.currentTimeMillis();
            String jiaminame = filename + "@" +  timestamp;

            String wjUrl = "D:"+File.separator+"xjzp" +File.separator + "dr";
            String bsUrl = wjUrl +File.separator +jiaminame +"." + fileType;

            Map<String,String> xsxx = xjzpService.getXsxx(filename);
            if(xsxx == null){
                throw new Exception();
            }

            String[] base64 =upload.split(",");
            byte[] base64Body = new BASE64Decoder().decodeBuffer(base64[1]);
            //byte[] base64Body = new BASE64Decoder().decodeBuffer(upload);
            XszpXxEntity xszpXxEntity = new XszpXxEntity();
            xszpXxEntity.setPylx("1");
            xszpXxEntity.setXjid(xsxx.get("ID"));
            xszpXxEntity.setZplxdm(xjzpPage.getZplx());
            xszpXxEntity.setZp(base64Body);
            xszpXxEntity.setZpbslj(bsUrl);
            xszpXxEntity.setZpmc(jiaminame + ".jpg");

            InetAddress ip = InetAddress.getLocalHost();
            String mac = getLocalMac(ip);
            xszpXxEntity.setIp(ip.toString());
            xszpXxEntity.setMac(mac);

            xszpXxEntity.setCjr(username);
            xszpXxEntity.setZhxgr(username);

            xjzpService.insertXjXszpxx(xszpXxEntity);
            boolean s = GenerateImage(base64[1],bsUrl,wjUrl);

            return "1";
        }catch (Exception e){
            e.printStackTrace();
            return "2";
        }
    }
public  boolean GenerateImage(String imgStr, String imgFilePath,String wjUrl) {
        if (imgStr == null)// 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            File file = new File(wjUrl);
            //如果目录不存在则创建
            if(!file.isDirectory()){
                file.mkdirs();
            }
            OutputStream out = new FileOutputStream(new File(imgFilePath));
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.getMessage();
            return false;
        }
    }

前端查看照片

@SuppressWarnings("all")
    @RequestMapping(value = "ckXszp" , method = RequestMethod.POST )
    public Map ckXszp(@AuthenticationPrincipal UserDetails user, XjzpPage xjzpPage,HttpServletRequest request ) throws SQLException, UnsupportedEncodingException {
        Map map=new HashMap();
        List<Map<String,String>> zplxList = xjzpService.queryZplx();
        String xjid = xjzpService.getXjid(xjzpPage);
        xjzpPage.setXjid(xjid);
        List<XszpXxEntity> zpList = xjzpService.queryZp(xjzpPage);

        if(zpList.size()>0){
            for (int i = 0; i < zpList.size(); i++) {
                XszpXxEntity xszpXxEntity =  zpList.get(i);
                byte[] bdata = xszpXxEntity.getZp();
                BASE64Encoder encode = new BASE64Encoder();
                String s = encode.encode(bdata);
                s = "data:image/jpeg;base64," + s;
                xszpXxEntity.setZpstr(s);
            }
        }
        map.put("zplxList",zplxList);
        map.put("zpList",zpList);

        return map;

    }

2、get方法请求下载导出

@GetMapping(value = "exportPhotos")
    @Transactional
    public void exportPhotos(XjzpPage xjzpPage,String ids,boolean pldc, HttpServletRequest request, HttpServletResponse response) {
        try {
            if(pldc == true){
                String[] xhs = ids.split(",");
                for (int i = 0; i < xhs.length; i++) {
                    String xh = xhs[i];
                    xjzpPage.setXh(xh);
                    List<XjzpXsEntity> xsList = xjzpService.queryBks(xjzpPage);
                    for (int j = 0; j < xsList.size(); j++) {
                        XjzpXsEntity xjzpXsEntity =  xsList.get(j);
                        xjzpPage.setXjid(xjzpXsEntity.getId());

                        List<XszpXxEntity> xszpXxEntity = xjzpService.queryZp(xjzpPage);
                        String xjid = "";
                        for (int k = 0; k < xszpXxEntity.size(); k++) {
                            XszpXxEntity xxEntity =  xszpXxEntity.get(k);
                            xjid += xxEntity.getXjid() + ",";


                            /*response.setContentType("jpeg/img/jpg/png");
                            byte[] bytes = xxEntity.getZp();
                            OutputStream outputStream = response.getOutputStream();
                            response.setHeader("Content-Disposition", "attachment; filename=" + xxEntity.getZpmc());
                            InputStream in = new ByteArrayInputStream(bytes);

                            try {
                                int len = 0;
                                byte[] buf = new byte[1024];
                                while ((len = in.read(buf, 0, 1024)) != -1) {
                                    outputStream.write(buf, 0, len);
                                }
                                outputStream.flush();
                                outputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                if (outputStream != null) {
                                    outputStream.flush();
                                    outputStream.close();
                                }
                                if (in != null) {
                                    in.close();
                                }
                            }*/



                            /*BASE64Encoder encode = new BASE64Encoder();
                            String s = encode.encode(bytes);
                            String wjUrl = "D:"+File.separator+"xjzp" +File.separator + "dc";
                            String bsUrl = wjUrl +File.separator + xxEntity.getZpmc();

                            GenerateImage(s,bsUrl,wjUrl);*/
                        }
                        String xjid1 = xjid.substring(0,xjid.length() -1);
                        String[] xjids = xjid1.split(",");


                        File path = new File(ResourceUtils.getURL("classpath:").getPath());
                        File upload = new File(path.getAbsolutePath(), "templates/temp/");
                        export2zip(request, response, upload,xxEntity.getZpmc());

                        //删除临时目录文件
                        File[] tempList = upload.listFiles();
                        if (tempList != null) {
                            for (File file : tempList) {
                                if (file.isFile()) file.delete();
                            }
                        }

                    }
                }
            }else{
                List<XjzpXsEntity> xsList = xjzpService.queryBks(xjzpPage);

            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无极的移动代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值