spring mvc 文件上传 前台jsp图片传不到后台的原因

前台jsp

<form action="${pageContext.request.contextPath}/file/upload" method="post" >
    <input type="file" name="file"/>
    <button type="submit">上传</button>
    </form>

后台接受不到  上传文件要特殊定义

加上enctype="multipart/form-data"就好使了

  <form action="${pageContext.request.contextPath}/file/upload" method="post"enctype="multipart/form-data" >
    <input type="file" name="file"/>
    <button type="submit">上传</button>
    </form>

 

后台代码

/***
     * 保存图片文件
     * @param file
     * @return
     */
    private String saveFile(HttpServletRequest request, MultipartFile file,
            String fileName) {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        String filePath = "";
        String Path = "";
        //判断文件是否为空
        if (!file.isEmpty()) {
            try {
                filePath =  getPath(request)
                        + "/upload/"+ df.format(new Date())+ "/"
                        + sdf.format(new Date()) + "/" + fileName;
                Path = "/upload/"+ df.format(new Date())+ "/"+ sdf.format(new Date()) + "/" + fileName;
                System.err.println(filePath);
                File saveDir = new File(filePath);
                if (!saveDir.getParentFile().exists())
                    saveDir.getParentFile().mkdirs();
                // 转存文件
                file.transferTo(saveDir);
                // 压缩大小
                Thumbnails.of(saveDir).size(800, 600).toFile(saveDir);
                return Path;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Path;
    }

    @ResponseBody
    @RequestMapping("/upload")
    public Json filesUpload(@RequestParam("file") MultipartFile file,Photos photos,
            HttpServletRequest request) {
        Json json = new Json();
        // 保存文件
        /*5017155
         * String fileName = System.currentTimeMillis() +
         * file.getOriginalFilename().substring(
         * file.getOriginalFilename().lastIndexOf("."));
         */
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String fileName = df.format(new Date())
                + file.getOriginalFilename().substring(
                        file.getOriginalFilename().lastIndexOf("."));
        
        String filePath = saveFile(request, file, fileName);
        String realpath = getPath(request);
        String path = realpath+"/" + filePath;
        
        if(photos.getPhotoone()!=null&&!"".equals(photos.getPhotoone())){
            File fil = new File(realpath+photos.getPhotoone());
            if(fil.exists()){
                fil.delete();
            }
        }
        if (filePath != null || !filePath.equals("")) {
            json.setSuccess(true);
            json.setMsg(filePath);
            json.setImg(path);
        } else {
            json.setSuccess(false);
            json.setMsg("上传失败");
        }
        return json;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值