02.springmvc实现文件上传

1.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>







 test file upload    

   
    
parameter1:

parameter1:



2.controller文件:
        @RequestMapping(value = "/uploadCourseCover", method = RequestMethod.POST)
    public @ResponseBody ReturnData uploadCourseCover(@RequestParam("file") MultipartFile file, HttpServletRequest request ) throws CustomException {
        ReturnData rd = new ReturnData();
                String parameter1 = request.getParameter("parameter1");
        String parameter2 = request.getParameter("parameter2");
        String picUrl = courseRepositoryService.upload(file,request);
        rd.setCode(Conss.RETURNDATA_SUCCEEDED);
        rd.setDesc("upload success");
        rd.setData(picUrl);
        return rd;
    }
3.service文件:
public String upload(MultipartFile file, HttpServletRequest request) {
        String absolutePath = null;
        if (!file.isEmpty()) {
            ServletContext sc = request.getSession().getServletContext();
                        // 设定文件保存的目录
            String dir = sc.getRealPath("/img"); 
                        // 得到上传时的文件名
            String fileName = file.getOriginalFilename();
            String strs[] = fileName.split("\\.");
            fileName = "";
            for(int i=0;i
4.文件下载:

http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html
http://www.2cto.com/kf/201409/331289.html

5.读取文件流在页面输出显示--(防止看到文件路径)

http://blog.csdn.net/woweipingzui/article/details/51037753

你可能感兴趣的:(02.springmvc实现文件上传)