package com.fgkj.controller; import com.fgkj.dto.*; import com.fgkj.mapper.UinfDaoFactory; import com.fgkj.services.User_logService; import com.fgkj.util.*; import com.google.gson.Gson; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; @RequestMapping("myFile") @RestController @Api(tags = "myFile接口") public class MyFileController{ /*private String battname; private File[] file; // 提交过来的file的名字 private String[] fileFileName; // 提交过来的file的MIME类型 private String[] fileContentType; private String filestr; //文件内容 private String fname; //文件内容*/ //上传文件 @PostMapping("/uploadTestFile") @ApiOperation(notes = "",value="byInfo") public ServiceModel UploadTestFile(@RequestParam File[] file,String[] fileFileName,String battName){ HttpServletResponse res = ActionUtil.getResponse(); res.setHeader("Access-Control-Allow-Origin", "*"); //允许跨域访问 res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token"); res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH"); boolean f = createFile(file,fileFileName,battName); ServiceModel model = new ServiceModel(); if(f){ model.setCode(1); model.setMsg("上传成功"); }else{ model.setCode(0); model.setMsg("上传失败"); } return model; } public boolean createFile(File[] file,String[] fileFileName,String battName){ String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); boolean flag = false; String root = str+"/uploadfiles/"; // 上传路径 //System.out.println(battname+"***"); for(int i=0;i0){ List myfiles = new ArrayList(); for(int i=0;i 0){ model.setCode(1); model.setData(myfiles); model.setMsg("读取成功"); } } } return model; } //将文件复制到指定的目录 public static void copyFile(File oldFile, String newPath){ File file = new File(newPath); FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(oldFile); out = new FileOutputStream(file);; byte[] buffer=new byte[2097152]; int readByte = 0; while((readByte = in.read(buffer)) != -1){ out.write(buffer, 0, readByte); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } //上传机房视频接口(手机端) @PostMapping("uploadStationFile4Mobile") @ApiOperation(notes = "",value="上传机房视频接口(手机端)") public ServiceModel uploadStationFile_mobile(@RequestBody BattInf binf,@RequestParam File[] file,String[] fileFileName){ // BattInf binf = getGson().fromJson(json, BattInf.class); String fileRoot = "stationsrc"; ServiceModel model = new ServiceModel(); boolean isSuccess = false; String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); //获取服务器所在的绝对路径 String root = str+"/"+fileRoot+"/"+binf.getStationId()+"/"+binf.getVideoOrImage()+"/"; // 上传路径 for(int i=0;i设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) //设置经度 g.drawString("经度:"+binfmation.getLongitude(), 0 , font_size); //设置纬度 g.drawString("纬度:"+binfmation.getLatitude(), 0 , font_size*2); //设置地址 String address="地址:"+binfmation.getAddress(); int address_size=getTextLength(address)*font_size; if(address_size1) { length++; } } length = length%2==0?length/2:length/2+1; return length; } //读取文件(图片)长,宽,图片名字 public static ImageSize ImageInfor(File file) { ImageSize ims=new ImageSize(); java.awt.Image srcImg; try { srcImg = ImageIO.read(file); ims.setHight(srcImg.getHeight(null));//文件的高 ims.setWidth(srcImg.getWidth(null));//文件的宽 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ims; } /** * 读取所有上传过图片和视频的机房id文件夹 * @return */ @GetMapping("readAllStation") @ApiOperation(notes = "",value="读取所有上传过图片和视频的机房id文件夹") public ServiceModel ReadAllStation() { String fileRoot = "stationsrc"; ServiceModel model = new ServiceModel(); String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); //获取服务器所在的绝对路径 String root = str+"/"+fileRoot+"/"; // 上传路径 File files = new File(root); File[] sourses = files.listFiles(); List allfiles = new ArrayList(); for (int i = 0; i < sourses.length; i++) { if(sourses[i].isDirectory()) { allfiles.add(sourses[i]); } } if(allfiles.size()>0) { model.setCode(1); model.setData(allfiles); }else { model.setCode(0); model.setMsg("暂无机房资源"); } return model; } /** * 删除指定的资源文件 * @return */ @DeleteMapping("file") @ApiOperation(notes = "",value="删除指定的资源文件") public ServiceModel deleteFile(@RequestBody MyFile myFile) { ServiceModel model = new ServiceModel(); // MyFile myfile = getGson().fromJson(json, MyFile.class); List listU=new ArrayList();//存放操作记录 String fileRoot = "stationsrc"; String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); //获取服务器所在的绝对路径 String targetFilePath = str+"/"+fileRoot+"/"+myFile.getFilePath()+"/"+myFile.getFileType()+"/"+myFile.getFileName(); // 上传路径 File file = new File(targetFilePath); //System.out.println(targetFilePath); if(file.exists() && file.isFile()) { try { file.delete(); model.setCode(1); model.setMsg("删除成功"); } catch (Exception e) { model.setMsg("删除失败"); e.printStackTrace(); } { String msg=((User_inf)ActionUtil.getUser()).getuName()+"删除"+myFile.getFilePath()+"机房的"+myFile.getFileName(); User_log ulog=UinfDaoFactory.CreateULog(UinfDaoFactory.Delete, msg); listU.add(ulog); (new User_logService()).addPro(listU);//将用户的操作记录下来 } }else { model.setMsg("文件不存在"); } return model; } /* public static void main(String[] args) { *//*String filePath = "D:/test/a/a.txt"; //ActionUtil.createFileRootIFNotExist(filePath); File file = new File(filePath); System.out.println(ActionUtil.tojson(file));*//* //GraphicsFile("我是鲁星伟","D:\\1.jpg","D:\\2.jpg",0,new Color(255,0,0),"JPG"); BattMap_information binfmation=new BattMap_information(); binfmation.setStationId("42000001"); binfmation.setStationName("广西省-百色-GX百色乐业县立新小区WF-BTS设备"); binfmation.setAddress("百色市乐业县立新小区附近居民楼上"); binfmation.setLongitude(106.55); binfmation.setLatitude(24.78); File file=new File("D:\\1.jpg"); String filePath = "D:\\2.jpg"; //String filename="2.jpg"; GraphicsFile(binfmation,file,filePath); }*/ }