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. Java httpclient请求,解决乱码问题

    public class HttpPostRequestUtil { public HttpPostRequestUtil() { } public static String post(String ...

  2. 基于Qt的遥感图像处理软件设计总结

     开发工具 VS2008+Qt4.8.0+GDAL1.9  要点 接口要独立,软件平台与算法模块独立,平台中各接口设计灵活,修改时容易. 设计软件时一步步来,每个功能逐一实现,某个功能当比较独立时可以 ...

  3. ip校验方法:判断ip是否位于指定的范围内

    import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.reg ...

  4. Visual Studio 2013 prerequisites

    http://www.visualstudio.com/zh-cn/products/visual-studio-ultimate-with-msdn-vs#Fragment_SystemRequir ...

  5. Xshell中文乱码问题

    1.  先查看当前使用的语言: echo $LANG 2.  查看系统的语言安装包: locale 3.  如果没有中文安装包(包含zh_CN字样),需要网络或者自己上传安装包,安装 4.  有了中文 ...

  6. Three levels at which any machine carrying out an Information-Processing task must be understood

    1. Computational theory What is the goal of computation, why is it appropriate, and what is the logi ...

  7. Generating a new ASP.NET session in the current HTTPContext

    void regenerateId() { System.Web.SessionState.SessionIDManager manager = new System.Web.SessionState ...

  8. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】连载导读

    前言: 无数昼夜的来回轮替以后,这本<驱动篇I>终于编辑完毕了,笔者真的感动到连鼻涕也流下来.所谓驱动就是认识硬件,还有前期建模.虽然<驱动篇I>的硬件都是我们熟悉的老友记,例 ...

  9. A little problem for pt-pmp

    https://bugs.launchpad.net/percona-toolkit/+bug/1320168 We use the pt-pmp (a variety for pmp !http:/ ...

  10. IE11兼容性设定

    增加browser文件,如下:  <browsers>   <browser refID="Default">     <capabilities&g ...