package com.mandi.servlet.controller; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.mandi.common.SessionMethod; /** * @author mengly * @version 创建时间:2016年4月15日 下午7:28:44 * 类说明 */ @Controller("fileupload") @RequestMapping(value="/file/download",method={RequestMethod.GET,RequestMethod.POST}) public class Fileupload { @Resource(name="multipartResolver") private CommonsMultipartResolver cmr; @RequestMapping(value = "/up.htm", method = {RequestMethod.POST, RequestMethod.GET }) @ResponseBody public String up(HttpServletRequest r,HttpServletResponse re) { if(cmr.isMultipart(r)) { @SuppressWarnings("unused") MultipartHttpServletRequest mhsr=(MultipartHttpServletRequest) r; }else{ } return null; } @RequestMapping(value = "/ups.htm", method = {RequestMethod.POST, RequestMethod.GET }) @ResponseBody public String ups(HttpServletRequest r,HttpServletResponse re,@RequestParam("file") MultipartFile[] mfs) { JsonArray ja=new JsonArray(); Gson g=new Gson(); for(MultipartFile mf:mfs){ String rf=r.getServletContext().getRealPath("/imgs/adsense/imgs/"); File f=new File(rf); if(!f.exists()) f.mkdirs(); if(!rf.equals(File.separator)){ rf+=File.separator; } String tname=UUID.randomUUID().toString()+mf.getOriginalFilename(); Path p=Paths.get(rf+tname); JsonObject jo=new JsonObject(); try { Files.copy(mf.getInputStream(), p); jo.addProperty("url", p.toString()); jo.addProperty("size", "1024"); jo.addProperty("name1", mf.getOriginalFilename()); ja.add(jo); } catch (IOException e) { e.printStackTrace(); SessionMethod.writeresp(re, g.toJson(jo)); return null; } } SessionMethod.writeresp(re, g.toJson(ja)); return null; } }