|
发表于 2013-5-4 21:45:25
|
显示全部楼层
本帖最后由 GIN 于 2013-5-4 21:46 编辑
- /**
- * 上传文件
- *
- * @param request
- * @return
- * @throws Exception
- */
- @RequestMapping(params = "saveFiles", method = RequestMethod.POST)
- @ResponseBody
- public AjaxJson saveFiles(HttpServletRequest request) throws Exception {
- // 把压缩包里面的jar拷贝到工程的lib下面即可 主要方法介绍
- // 新建一个SmartUpload对象
- AjaxJson j = new AjaxJson();
- String fileName = null;
- UploadFile uploadFile = new UploadFile(request);
- String ctxPath = request.getSession().getServletContext().getRealPath("");
- File file = new File(ctxPath);
- if (!file.exists()) {
- file.mkdir();// 创建文件根目录
- }
- MultipartHttpServletRequest multipartRequest = uploadFile.getMultipartRequest();
- Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
- for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
- MultipartFile mf = entity.getValue();// 获取上传文件对象
- fileName = mf.getOriginalFilename();// 获取文件名
- String savePath = file.getPath() + "/" + fileName;// 上传后的文件绝对路径
- System.out.println("上传后路径:"+savePath);
- File savefile = new File(savePath);
- try {
- FileCopyUtils.copy(mf.getBytes(), savefile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- message = "上传成功";
- j.setMsg(message);
- return j;
- }
复制代码 |
|