springMVC基础

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基础的更多相关文章
- springMVC基础controller类
此文章是基于 搭建SpringMVC+Spring+Hibernate平台 功能:设置请求.响应对象:session.cookie操作:ajax访问返回json数据: 创建springMVC基础con ...
- SpringMVC基础入门
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- SpringMVC基础配置及使用
SpringMVC基础配置及使用 SpringMVC:1.SpringMVC和Spring的关系: 软件开发的三层架构: web层[表示层.表现层]---->Service层----> ...
- SpringMVC基础01——SpringMVC的知识结构体系
1.前言 目前在各大互联网公司使用最热门的技术莫过于SpringBoot以及在其基础之上的SpringCloud,然而学习这些技术的基础在于学好Spring和SpringMVC,准确来说SpringM ...
- SpringMVC基础配置(通过注解配置,非xml配置)
SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...
- SpringMVC基础(二)_文件上传、异常处理、拦截器
实现文件上传 实现文件上传,需要借助以下两个第三方 jar 包对上传的二进制文件进行解析: commons-fileupload commons-io form表单的 enctype 取值必须为:mu ...
- JavaEE SpringMVC 基础概念(如需详细资料请留言)
SpringMVC Web开发核心思想: 模型与视图相分离: 控制逻辑与业务逻辑相分离: 视图选择与具体视图技术相分离: 非侵入式开发(尽量少使用框架特定API),易于测试. SpringMVC是什么 ...
- springMVC基础配置
web.xml文件 <?xml version="1.0" encoding="UTF-8"?><web-app version= ...
- SpringMVC基础——@ModelAttribute和@SessionAttribute
一.@ModelAttribute 注解 对方法标注 @ModelAttribute 注解,在调用各个目标方法前都会去调用 @ModelAttribute 标记的注解.本质上来说,允许我们在调用目标方 ...
随机推荐
- Page和UserControl的事件加载顺序
UC OnInitPage OnInit Page Page_LoadPage OnLoad UC Page_LoadUC OnLoad Page OnPreRenderUC OnPreRender
- mycat服务启动{管理模块启动过程}
mycat启动的时候启动了三个模块 1:NIOConnector(负责链接mysql数据库,连接池以数据库为准不以链接字符串为准), 1:NIOAcceptor,ManagerConnectionFa ...
- 内存的分配VS回收&构造函数VS析构函数
之前有一个问题一直困扰着我,就是一个变量出了作用域,我以为这个变量的内存就被回收了,其实不是这样的,昨天问了一个高手,才豁然开朗,自己在看相关代码的反汇编代码,才知道原来真是这样就.这个问题,我想简单 ...
- O2O地图应用之判断用户订单地址是否在服务范围内
O2O地图应用之判断用户订单地址是否在服务范围内 需求分析 在o2o项目中,经常要用到在用户下单时判断用户所填地址的坐标点是否在服务范围内的情况,这里参考网上的实现方式,用C#来实现,经测试后有效,特 ...
- [转载] Calculating Entropy
From: johndcook.com/blog For a set of positive probabilities pi summing to 1, their entropy is defi ...
- Entity Framework 在Vs2012下Update Model From DataBase 失败的问题
http://stackoverflow.com/questions/13054212/vs-2012-ef-5-0-update-model-from-database-not-picking-up ...
- VirtualBox的四种网络连接方式
VirtualBox中有4中网络连接方式:a. NAT 网络地址转换模式(Network Address Translation)b. Bridged ...
- [GraphQL] Use GraphQLNonNull for Required Fields
While certain fields in a GraphQL Schema can be optional, there are some fields or arguments that ar ...
- [转]ASP.NET页面之间传递值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...
- LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...