miniui图片附件上传回显

5 篇文章 0 订阅
2 篇文章 0 订阅
/**
     * @Author zhangwei
     * @Description //TODO 图片附件上传
     * @Date 9:30 2020/1/3
     * @Param [fileUpload1, installNo, type]
     * @return java.lang.String
     **/
    @RequestMapping("/upload")
    public String upload(
            @RequestParam(value = "fileUpload1",required = false) MultipartFile fileUpload1,
            @RequestParam(value = "installNo",required = false)String installNo,
            @RequestParam(value = "type",required = false)String type){
        Operator principal = (Operator) SecurityUtils.getSubject().getPrincipal();
        InstallPath installPath = new InstallPath();
        installPath.setInstallPathOperason(principal.getOperatorId());
        installPath.setCompanyId(principal.getCompanyId());
        installPath.setInstallPathType(type);
        installPath.setInstallNo(installNo);
        installPath.setInstallPathStatus("1");
        if (type.equals("1")){
            installPath.setInstallPathOtherName("书面申请");
        }
        if (type.equals("2")){
            installPath.setInstallPathOtherName("身份证明");
        }
        if (type.equals("3")){
            installPath.setInstallPathOtherName("建筑许可");
        }
        if (type.equals("4")){
            installPath.setInstallPathOtherName("建筑平面");
        }
        if (type.equals("5")){
            installPath.setInstallPathOtherName("勘察报告");
        }
        if (type.equals("6")){
            installPath.setInstallPathOtherName("预算报告");
        }
        if (type.equals("7")){
            installPath.setInstallPathOtherName("审核报告");
        }
        if (type.equals("8")){
            installPath.setInstallPathOtherName("实施报告");
        }
        if (type.equals("9")){
            installPath.setInstallPathOtherName("现场施工");
        }
        if (type.equals("10")){
            installPath.setInstallPathOtherName("验收报告");
        }
        if(fileUpload1.isEmpty()){
            return "文件为空";
        }
        //获取文件名
        String fileName = fileUpload1.getOriginalFilename();
        //时间戳
        fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;
        installPath.setInstallPathName(fileName);
        //服务器路径
        //String path = "/home/Picture/" + fileName;
        String path = "/home/attach/pic/" + fileName;
        //本地路径
        //String path = "F:/img/" + fileName;
        File dest = new File(path);
        //文件是否存在
        boolean b = installService.findOldUrl(installNo, type, principal.getCompanyId());
        if (b == false) {
            return "已存在附件但覆盖失败,请稍后再尝试上传!";
        }
        //文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            fileUpload1.transferTo(dest);
            String url = request.getServerName()
                    + ":"
                    + request.getServerPort()
                    +path;
            installPath.setInstallPathUrl(path);
            installService.saveUrl(installPath);

        } catch (IOException e) {
            //e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";
    }

    /**
     * @Author zhangwei
     * @Description //TODO 读取图片
     * @Date 9:46 2019/12/31
     * @Param [response, path]
     * @return void
     **/
    @GetMapping("/uploadPic")
    public void uploadPic(HttpServletResponse response,
                          @RequestParam(value = "path",required = false)String path) {
        //String path = "F:\\img\\20191227095132_建筑许可证.jpg";
        File file = new File(path);
        try {
        if (file.exists()) {
            InputStream in = new FileInputStream(path);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            while (in.read(b) != -1) {
                os.write(b);
            }
            in.close();
            os.flush();
            os.close();
        }
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
//上传成功
function onUploadSuccess(e) {
    alert(e.serverData);
    this.setText("");
}

//上传失败
function onUploadError(e) {
    alert(e.serverData)
}

// 提交
$('#importBtn').click(function() {
    mini.get("fileUpload1").setPostParam({installNo:GLOBAL.apply.installNo,type:7});
    mini.get("fileUpload1").startUpload();
});


function setPicture(data) {
    var identityCardImg = document.getElementById("identityCardImg");
    var planAllowImg = document.getElementById("planAllowImg");
    var buildingPictureImg = document.getElementById("buildingPictureImg");
    var url = "/install/installation/uploadPic";
    $.post("/install/installation/findAttach", {installNo:data.installNo},function (text,status) {
        var o = mini.decode(text);
        identityCardImg.src = url+"?path="+o[0].applyUrl;
        planAllowImg.src = url+"?path="+o[0].permitUrl;
        buildingPictureImg.src = url+"?path="+o[0].buildingPlaneUrl;
    });
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 提供了文件上传的支持,可以使用 MultipartFile 类型来接收上传的文件。以下是图片上传与回的步骤: 1. 在前端页面中添加一个表单,其中包含一个文件选择器用于选择要上传的图片文件,并设置表单的 enctype 为 "multipart/form-data"。 2. 在后端的控制器中定义一个处理文件上传的方法,并使用 @RequestParam 注解来接收上传的图片文件。例如: ``` @RequestMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { // 处理上传的文件 return "redirect:/image"; // 重定向到图片的方法 } ``` 3. 在处理上传的方法中可以进行文件的存储操作,例如将文件保存到本地的文件系统或将文件存储到数据库。 4. 在图片的方法中,可以通过文件的路径或存储的唯一标识来获取图片,并将图片返回给前端页面。例如: ``` @RequestMapping("/image") public void showImage(HttpServletResponse response) throws IOException { // 从存储位置获取图片,如从文件系统获取或从数据库获取 File imageFile = new File("path/to/image.jpg"); // 设置响应的 Content-Type 为 image/jpeg response.setContentType(MediaType.IMAGE_JPEG_VALUE); // 将图片写入响应的输出流中 FileInputStream fileInputStream = new FileInputStream(imageFile); IOUtils.copy(fileInputStream, response.getOutputStream()); fileInputStream.close(); response.getOutputStream().close(); } ``` 5. 在前端页面中,可以使用 img 标签来图片,设置其 src 属性为图片的方法的 URL。例如: ``` <img src="/image" alt="Image"> ``` 通过以上步骤,我们可以实现图片上传与回的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值