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基础的更多相关文章

  1. springMVC基础controller类

    此文章是基于 搭建SpringMVC+Spring+Hibernate平台 功能:设置请求.响应对象:session.cookie操作:ajax访问返回json数据: 创建springMVC基础con ...

  2. SpringMVC基础入门

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  3. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  4. SpringMVC基础配置及使用

    SpringMVC基础配置及使用 SpringMVC:1.SpringMVC和Spring的关系:    软件开发的三层架构: web层[表示层.表现层]---->Service层----> ...

  5. SpringMVC基础01——SpringMVC的知识结构体系

    1.前言 目前在各大互联网公司使用最热门的技术莫过于SpringBoot以及在其基础之上的SpringCloud,然而学习这些技术的基础在于学好Spring和SpringMVC,准确来说SpringM ...

  6. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  7. SpringMVC基础(二)_文件上传、异常处理、拦截器

    实现文件上传 实现文件上传,需要借助以下两个第三方 jar 包对上传的二进制文件进行解析: commons-fileupload commons-io form表单的 enctype 取值必须为:mu ...

  8. JavaEE SpringMVC 基础概念(如需详细资料请留言)

    SpringMVC Web开发核心思想: 模型与视图相分离: 控制逻辑与业务逻辑相分离: 视图选择与具体视图技术相分离: 非侵入式开发(尽量少使用框架特定API),易于测试. SpringMVC是什么 ...

  9. springMVC基础配置

    web.xml文件       <?xml version="1.0" encoding="UTF-8"?><web-app version= ...

  10. SpringMVC基础——@ModelAttribute和@SessionAttribute

    一.@ModelAttribute 注解 对方法标注 @ModelAttribute 注解,在调用各个目标方法前都会去调用 @ModelAttribute 标记的注解.本质上来说,允许我们在调用目标方 ...

随机推荐

  1. netfilter分析

    转自:http://blog.sina.com.cn/s/blog_a31ff26901013n07.html 一.概述 1. Netfilter/IPTables框架简介 Netfilter/IPT ...

  2. 在gradle 中使用ant 执行 “命令行”(CMD)不出日志解决方案

    因为gradle 好恶心,声明的任务,一定会事先运行一次,而任务追加的话就不会 例如: task hello(){ println "HelloWorld" } task hell ...

  3. Javascript - 栈 和 单链表

    最近在重温数据结构,于是写了一些代码玩玩,都是很初级的,表喷各位.... function Stack() { this.dataStore = []; this.top = 0; } Stack.p ...

  4. WCF 数据服务 4.5

    .NET Framework 4.5 其他版本 WCF 数据服务(以前称为"ADO.NET Data Services")是 .NET Framework 的一个组件.可以使用此组 ...

  5. parallels无法启动之大乌龙-流水账版

    欢迎访问我的blog:blog.thinkinside.me     早上到公司,像往日一样,开电脑倒茶喝水. 回到座位打开parallels desktop,发现不对,打开PD非常的慢.显示正在初始 ...

  6. [Lua]50行代码的解释器,用来演示lambda calculus

    嗯,来写写经过: 在知乎上看见用Belleve牛用javascript写了一个精简的lisp解释器 => 我也想写一个,用lua写,能多简单呢? => 写了一个阉割的scheme解释器,包 ...

  7. mysql数据库导入和导出

    Mysql数据中,使用时,总是会碰见导入和导出情况,所以如何正确的导入导出,非常重要!下面根据工作中用到的方法,会不管补充: 导入: 直接在Mysql中导入: mysql>use databas ...

  8. A/B测试

    昨天把前段时间开发的二胡调音器的应用发布到了亚马逊应用程序商店,看到了一个A/B测试的标签,了解一下A/B测试的工作原理. A/B测试是一种新兴的网页优化方法,可以用于增加转化率注册率等网页指标. 使 ...

  9. 关于移动App的五个提问

    1.你的移动App利用了手机的哪些特性? 2.你们是否有用移动的角度和思维来考虑产品形态?还是简单的把Web照搬到手机上? 3.用户有什么特殊的动力去安装你们的App? 4.用户是否能很好的上手和使用 ...

  10. MyEclipse设置编码方式

    1.windows->Preferences……打开"首选项"对话框,左侧导航树,导航到general->Workspace, 右侧Text file encoding ...