spring mvc上传文件

    @ResponseBody
    @RequestMapping("/upload")
    public String upload(HttpServletRequest req) {
        try {
            MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) req;
            MultipartFile file = mreq.getFile("file");
            String fileName = file.getOriginalFilename();
            FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/") +
                    "upload/" + fileName);
            fos.write(file.getBytes());
            fos.flush();
            fos.close();
            return "success";
        } catch (IOException e) {
            return e.getMessage();
        }
    }
posted @ 2019-04-25 14:55  小老弟  阅读(135)  评论(0编辑  收藏  举报