SSM + AJAX + FormData上传文件

不废话,直接上代码

书名: ISBN:
作者: 封面:
是否热门: 简介:
 @RequestMapping("/addBook")
    @ResponseBody
    public Book addBook(BookCustom bookCustom) throws Exception {
        System.out.println(bookCustom);
        String originalFilename = bookCustom.getMultipartFile().getOriginalFilename();
        String filename = UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String fpath = path + filename;
        BookQueryVo bookQueryVo = new BookQueryVo();
        bookCustom.setPic(filename);
        bookQueryVo.setBookCustom(bookCustom);
        bookService.insertBook(bookQueryVo);
        File file = new File(fpath);
        bookCustom.getMultipartFile().transferTo(file);
        Book book = new Book();
        BeanUtils.copyProperties(bookCustom,book);
        return book;
    }

注意一点就是$(#btn_submit)只是单纯的button元素(没有在表单内!),如果放在表单内或者直接使用submit类型则回自动提交表单,收到json数据后跳转页面,直接显示json数据,达不到刷新页面数据的效果。

你可能感兴趣的:(java,Ajax)