码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC基础

时间:2015-03-04 16:15:59      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

controllers包写控制器:

@Controller  
@RequestMapping(value="/utils")  
public class UploadController{  
    /** 
     * 上传文件 
     * @return 
     * @throws IOException  
     * @throws IllegalStateException  
     */  
    @RequestMapping(value = "/uploadify", method = RequestMethod.POST)  
    @ResponseBody  
    public String upload(HttpServletRequest request, HttpServletResponse response){  
        String responseStr="";  
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
  
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();    
        // 文件保存路径  ctxPat本地路徑
        String ctxPath=request.getSession().getServletContext().getRealPath("/")+File.separator+"uploadFiles";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");  
        String ymd = sdf.format(new Date());  
        ctxPath += File.separator + ymd + File.separator;  
        // 创建文件夹  
        File file = new File(ctxPath);    
        if (!file.exists()) {    
            file.mkdirs();    
        }    
        String fileName = null;    
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {    
            // 上传文件   
            MultipartFile mf = entity.getValue();  

            fileName = mf.getOriginalFilename();  
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();  
            // 重命名文件  
            //SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");  
            //String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;  
            //File uploadFile = new File(ctxPath + newFileName);    
            /**
             * modify,文件原名
             */
            File uploadFile = new File(ctxPath + fileName);
            try {  
                FileCopyUtils.copy(mf.getBytes(), uploadFile);  
                responseStr="上传成功";  
            } catch (IOException e) {  
                responseStr="上传失败";  
                e.printStackTrace();  
            }    
        }   
        return responseStr;  
    }  
}  

core包写Service接口

services包实现core中的接口,一般不写界面代码,主要和数据库打交道。

entity类是实体,类的字段

 

springMVC基础

标签:

原文地址:http://www.cnblogs.com/Teofil/p/4313456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!