package com.fgkj.controller; import com.fgkj.util.*; import com.fgkj.dto.User_Chart; import com.fgkj.services.User_ChartService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import sun.misc.BASE64Decoder; import java.io.*; import java.util.*; import javax.annotation.Resource; @RequestMapping("chart") @RestController @Api(tags = "chart接口") public class ChartController{ private static Map fileUtil = new HashMap(){ { put("logo","logo.png"); put("normal","home_normal.gif"); put("behind","home_behind.gif"); put("timeout","home_timeout.gif"); put("warn","home_warn.gif"); } }; @Resource private User_ChartService service; // private List files; // private List filesFileName; //上传文件内容类型集合  // private String josn; //执行上传功能 private void uploadFile(File file,String webpath) { InputStream in =null; OutputStream out =null; try { File fileLocation = new File(webpath).getParentFile(); //此处也可以在应用根目录手动建立目标上传目录 if(!fileLocation.exists()){ boolean isCreated = fileLocation.mkdir(); if(!isCreated) { //目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。 System.out.println("创建失败!"); } } in = new FileInputStream(file); File uploadFile = new File(webpath); out = new FileOutputStream(uploadFile); byte[] buffer = new byte[1024 * 1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } out.flush(); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { if (in!= null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } @PutMapping("form") @ApiOperation(notes = "",value="form") public boolean uploadform(@RequestBody User_Chart uchart,@RequestBody List files,@RequestBody List filesFileName){ // User_Chart uchart=ActionUtil.getGson().fromJson(json, User_Chart.class); boolean res=false; String webpath=null;//上传路径 //System.out.println(uchart); String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String root = new File(loadpath).getAbsolutePath(); System.out.println("root:"+root); for (int i = 0; i < files.size(); i++) { System.out.println("filename:"+filesFileName.get(i)); File file=files.get(i); //循环上传每个文件 webpath=root+"/images/"+uchart.getChart_file()+"/"+filesFileName.get(i); System.out.println(webpath); //uploadFile(file,webpath); } System.out.println("完成"); //model=service.add(uchart); res=true; return res; } //修改班组的logo @PutMapping("serverRootLogo") @ApiOperation(notes = "",value="修改班组的logo") public boolean updateServerRootLogo(HashMap s){ boolean res=false; boolean isSuccess = false; //获取服务器上文件存放的根路径 String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); String root = str+"/images/"; // Map s = getGson().fromJson(json, new TypeToken>(){}.getType()); String dirname = s.get("chart_file"); if(dirname != null && dirname.length()>0){ root += dirname + "/"; } List bases = new ArrayList(); List filepath = new ArrayList(); Iterator iter = s.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); String fname = fileUtil.get(key); if(fname!=null && fname.length()>0){ bases.add(entry.getValue().toString()); filepath.add(root+fname); System.out.println(root+fname); } Object val = entry.getValue(); } //System.out.println(bases.size()); //System.out.println(filepath.size()); for(int i=0;i