| | |
| | | package com.fgkj.actions;
|
| | |
|
| | | import java.awt.AlphaComposite;
|
| | | import java.awt.Color;
|
| | | import java.awt.Graphics2D;
|
| | | import java.awt.RenderingHints;
|
| | | import java.awt.image.BufferedImage;
|
| | | import java.io.File;
|
| | | import java.io.FileInputStream;
|
| | | import java.io.FileNotFoundException;
|
| | | import java.io.FileOutputStream;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.OutputStream;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | |
|
| | | import javax.imageio.ImageIO;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | |
|
| | | import com.fgkj.dao.DAOHelper;
|
| | | import com.fgkj.dao.UinfDaoFactory;
|
| | | import com.fgkj.dto.ImageSize;
|
| | | import com.fgkj.dto.MyFile;
|
| | | import com.fgkj.dto.ServiceModel;
|
| | | import com.fgkj.dto.User_inf;
|
| | | import com.fgkj.dto.User_log;
|
| | | import com.fgkj.services.User_logService;
|
| | | import com.google.gson.Gson;
|
| | |
|
| | | public class MyFileAction extends ActionUtil{
|
| | | |
| | | private String result;
|
| | | private String json;
|
| | | private String battname;
|
| | | private File[] file;
|
| | | // 提交过来的file的名字
|
| | | private String[] fileFileName;
|
| | | // 提交过来的file的MIME类型
|
| | | private String[] fileContentType;
|
| | | |
| | | private String filestr; //文件内容
|
| | | private String fname; //文件内容
|
| | | |
| | | //上传文件
|
| | | public String UploadTestFile(){
|
| | | 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();
|
| | | ServiceModel model = new ServiceModel();
|
| | | if(f){
|
| | | model.setCode(1);
|
| | | model.setMsg("上传成功");
|
| | | }else{
|
| | | model.setCode(0);
|
| | | model.setMsg("上传失败");
|
| | | }
|
| | | result = ActionUtil.tojson(model);
|
| | | return SUCCESS;
|
| | | }
|
| | | |
| | | public boolean createFile(){
|
| | | 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;i<file.length;i++){
|
| | | String savepath=root+battname+"/"+fileFileName[i];
|
| | | //System.out.println(savepath);
|
| | | FileInputStream fis=null;
|
| | | FileOutputStream fos=null;
|
| | | try { |
| | | File f = new File(savepath);
|
| | | if(!f.exists()){
|
| | | if(!f.getParentFile().exists()){
|
| | | f.getParentFile().mkdirs();
|
| | | }
|
| | | f.createNewFile();
|
| | | }
|
| | | /** 创建输入流 */ |
| | | fis = new FileInputStream(file[i]); |
| | | /** 输出流 根据文件的路径+文件名称创建文件对象 */ |
| | | fos = new FileOutputStream(f); |
| | | /** 读取字节 */ |
| | | byte b[] = new byte[1024]; |
| | | int n = 0; |
| | | /** 读取操作 */ |
| | | while ((n = fis.read(b)) != -1) { |
| | | /** 写操作 */ |
| | | fos.write(b, 0, n); |
| | | } |
| | | fos.flush();
|
| | | flag = true;
|
| | | } catch (Exception e) { |
| | | e.printStackTrace();
|
| | | } finally{
|
| | | /** 关闭操作 */ |
| | | try {
|
| | | if (fis != null) { |
| | | fis.close(); |
| | | } |
| | | if (fos != null) { |
| | | fos.close(); |
| | | }
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | }
|
| | | return flag; |
| | | }
|
| | | |
| | | public String UploadIphoneFile(){
|
| | | 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");
|
| | | Gson gson = new Gson();
|
| | | int[] arr = gson.fromJson(filestr, int[].class);
|
| | | |
| | | System.out.println("文件名"+fname);
|
| | | System.out.println("文件内容:"+filestr);
|
| | | System.out.println("电池组名称"+battname);
|
| | | boolean f = createFileByarr(arr,fname,battname);
|
| | | |
| | | ServiceModel model = new ServiceModel();
|
| | | if(f){
|
| | | model.setCode(1);
|
| | | model.setMsg("上传成功");
|
| | | }else{
|
| | | model.setCode(0);
|
| | | model.setMsg("上传失败");
|
| | | }
|
| | | result = ActionUtil.tojson(model);
|
| | | return SUCCESS;
|
| | | }
|
| | | |
| | | /**
|
| | | * |
| | | * @param arr 文件的字节数组
|
| | | * @param fname 文件名
|
| | | * @param bname 电池组名称
|
| | | * @return 是否生成文件成功
|
| | | */
|
| | | public boolean createFileByarr(int[] arr,String fname,String bname){
|
| | | String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
|
| | | String str = new File(loadpath).getParentFile().getAbsolutePath();
|
| | | boolean flag = false;
|
| | | String root = str+"/uploadfiles/"; // 上传路径
|
| | | //System.out.println(battname+"***");
|
| | | String savepath=root+bname+"/"+fname;
|
| | | //System.out.println(savepath);
|
| | | FileOutputStream fos=null;
|
| | | byte[] bytes = new byte[arr.length];
|
| | | for(int i=0;i<arr.length;i++){
|
| | | bytes[i] = (byte)(arr[i] & 0xFF);
|
| | | } |
| | | |
| | | try { |
| | | File f = new File(savepath);
|
| | | if(!f.exists()){
|
| | | if(!f.getParentFile().exists()){
|
| | | f.getParentFile().mkdirs();
|
| | | }
|
| | | f.createNewFile();
|
| | | } |
| | | fos = new FileOutputStream(savepath);
|
| | | fos.write(bytes);
|
| | | fos.flush();
|
| | | flag = true;
|
| | | } catch (Exception e) { |
| | | e.printStackTrace();
|
| | | } finally{
|
| | | /** 关闭操作 */ |
| | | try {
|
| | | if (fos != null) { |
| | | fos.close(); |
| | | }
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | return flag; |
| | | }
|
| | | |
| | | //上传机房视频接口
|
| | | /*public String uploadStationFile(){
|
| | | 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<file.length && binf != null;i++){
|
| | | String filePath = root+fileFileName[i];
|
| | | createFilefolderIFNotExist(filePath);
|
| | | copyFile(file[i], filePath);
|
| | | isSuccess = true;
|
| | | }
|
| | | if(isSuccess){
|
| | | model.setCode(1);
|
| | | model.setMsg("上传成功");
|
| | | }else{
|
| | | model.setCode(0);
|
| | | model.setMsg("上传失败");
|
| | | }
|
| | | result = tojson(model);
|
| | | return SUCCESS;
|
| | | }
|
| | | */
|
| | | /**
|
| | | * |
| | | * 获取指定机房得资源
|
| | | */
|
| | | /*public String searchStationSource(){
|
| | | String fileRoot = "stationsrc"; //存放机房资源文件的文件夹
|
| | | BattInf binf = getGson().fromJson(json, BattInf.class);
|
| | | ServiceModel model = new ServiceModel();
|
| | | String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
|
| | | String str = new File(loadpath).getParentFile().getAbsolutePath(); |
| | | if(binf != null && binf.getStationId() != null && binf.getVideoOrImage() != null){
|
| | | String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getVideoOrImage()+File.separator; //当前请求的机房所在的资源文件夹
|
| | | //System.out.println(filesource);
|
| | | File source = new File(filesource);
|
| | | File[] files = source.listFiles();
|
| | | model.setMsg("暂无视频文件");
|
| | | if(files!=null && files.length>0){
|
| | | List myfiles = new ArrayList<MyFile>();
|
| | | for(int i=0;i<files.length;i++){
|
| | | MyFile myfile = new MyFile();
|
| | | if(files[i].isFile()){
|
| | | myfile.setFile(files[i]);
|
| | | if(binf.getVideoOrImage().equals("image")) {
|
| | | myfile.setIms(ImageInfor(files[i]));
|
| | | }
|
| | | myfiles.add(myfile);
|
| | | |
| | | }
|
| | | }
|
| | | if(myfiles.size() > 0){
|
| | | model.setCode(1);
|
| | | model.setData(myfiles);
|
| | | model.setMsg("读取成功");
|
| | | }
|
| | | }
|
| | | |
| | | }
|
| | | result = tojson(model);
|
| | | return SUCCESS;
|
| | | }*/
|
| | | |
| | | //将文件复制到指定的目录
|
| | | 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();
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | |
| | | //上传机房视频接口(手机端)
|
| | | /*public String uploadStationFile_mobile(){
|
| | | 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<file.length && binf != null;i++){
|
| | | String filePath = root+fileFileName[i];
|
| | | createFilefolderIFNotExist(filePath);
|
| | | if(binf.getVideoOrImage().equals("image")) {
|
| | | GraphicsFile(binf.getBinfmation(), file[i], filePath);
|
| | | }else {
|
| | | copyFile(file[i], filePath);
|
| | | }
|
| | | isSuccess = true;
|
| | | }
|
| | | if(isSuccess){
|
| | | model.setCode(1);
|
| | | model.setMsg("上传成功");
|
| | | }else{
|
| | | model.setCode(0);
|
| | | model.setMsg("上传失败");
|
| | | }
|
| | | result = tojson(model);
|
| | | return SUCCESS;
|
| | | }*/
|
| | | //将获取到得图片加水印
|
| | | /*public static void GraphicsFile(Object obj, File file,String newImagePath) {
|
| | | //InputStream is = null;
|
| | | OutputStream os = null;
|
| | | BattMap_information binfmation=(BattMap_information) obj;
|
| | | try {
|
| | | // 1、源图片
|
| | | java.awt.Image srcImg = ImageIO.read(file);
|
| | | //System.out.println(srcImg.);
|
| | | BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
|
| | | // 2、得到画笔对象
|
| | | Graphics2D g = buffImg.createGraphics();
|
| | | // 3、设置对线段的锯齿状边缘处理
|
| | | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
| | | g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
|
| | | // 4、设置水印旋转
|
| | | if (null != degree) {
|
| | | //System.out.println("buffImg.getWidth():"+buffImg.getWidth()+" buffImg.getHeight():"+buffImg.getHeight());
|
| | | g.rotate(Math.toRadians(degree), buffImg.getWidth()/10,buffImg.getHeight() /10);
|
| | | }
|
| | | // 5、设置水印文字颜色
|
| | | g.setColor(new Color(255,0,0));//默认红色
|
| | | int font_size= buffImg.getHeight() /35;
|
| | | // 6、设置水印文字Font
|
| | | g.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD,font_size));
|
| | | // 7、设置水印文字透明度
|
| | | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.9f));
|
| | | // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(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_size<buffImg.getWidth()) {
|
| | | g.drawString(address, 0 , font_size*(3));
|
| | | }else {
|
| | | for (int i = 0; i <=address_size/buffImg.getWidth(); i++) {
|
| | | if(i==0) {
|
| | | g.drawString(address.substring( 0,buffImg.getWidth()/font_size*(i+1)), 0 , font_size*(2+i+1));
|
| | | }else {
|
| | | if(i==address_size/buffImg.getWidth()) {
|
| | | g.drawString(address.substring( buffImg.getWidth()/font_size*(i),(address.length()) ), 0 , font_size*(5+i+1));
|
| | | }else{
|
| | | g.drawString(address.substring( buffImg.getWidth()/font_size*(i),buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1));
|
| | | }
|
| | | |
| | | }
|
| | | }
|
| | | }
|
| | | |
| | | |
| | | //设置时间
|
| | | g.drawString("时间:"+DAOHelper.sdf.format(new Date()), 0 , font_size*4);
|
| | | //设置机房id
|
| | | g.drawString("机房ID:"+binfmation.getStationId(), 0 , font_size*5);
|
| | | //设置备注
|
| | | String note="备注:"+binfmation.getStationName();
|
| | | int note_size=getTextLength(note)*font_size;
|
| | | //System.out.println(getTextLength(binfmation.getStationName()));
|
| | | //System.out.println("font_size:"+font_size+" note_size:"+note_size+" buffImg.getWidth():"+buffImg.getWidth());
|
| | | if(note_size<buffImg.getWidth()) {
|
| | | g.drawString(note, 0 , font_size*6);
|
| | | }else {
|
| | | for (int i = 0; i <=note_size/buffImg.getWidth(); i++) {
|
| | | if(i==0) {
|
| | | g.drawString(note.substring( 0,buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1));
|
| | | }else {
|
| | | if(i==note_size/buffImg.getWidth()) {
|
| | | g.drawString(note.substring( buffImg.getWidth()/font_size*(i),(note.length()) ), 0 , font_size*(5+i+1));
|
| | | }else{
|
| | | g.drawString(note.substring( buffImg.getWidth()/font_size*(i),buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1));
|
| | | }
|
| | | |
| | | }
|
| | | }
|
| | | }
|
| | | |
| | | // 9、释放资源
|
| | | g.dispose();
|
| | | // 10、生成图片
|
| | | os = new FileOutputStream(newImagePath);
|
| | | //System.out.println(os);
|
| | | ImageIO.write(buffImg, "JPG", os);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | } finally {
|
| | | try {
|
| | | if (null != is)
|
| | | is.close();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | try {
|
| | | if (null != os)
|
| | | os.close();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | |
| | | }*/
|
| | | //计算水印文本长度
|
| | | //1、中文长度即文本长度 2、英文长度为文本长度二分之一
|
| | | public static int getTextLength(String text){
|
| | | //水印文字长度
|
| | | int length = text.length();
|
| | |
|
| | | for (int i = 0; i < text.length(); i++) {
|
| | | String s =String.valueOf(text.charAt(i));
|
| | | if (s.getBytes().length>1) {
|
| | | 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
|
| | | */
|
| | | public String 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<File> allfiles = new ArrayList<File>();
|
| | | 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("暂无机房资源");
|
| | | }
|
| | | result = tojson(model);
|
| | | return SUCCESS;
|
| | | }
|
| | | |
| | | /**
|
| | | * 删除指定的资源文件
|
| | | * @return
|
| | | */
|
| | | public String deleteFile() {
|
| | | 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)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("文件不存在");
|
| | | }
|
| | | result = tojson(model);
|
| | | return SUCCESS;
|
| | | }
|
| | | |
| | | |
| | | public void setFilestr(String filestr) {
|
| | | this.filestr = filestr;
|
| | | }
|
| | |
|
| | | public void setFname(String fname) {
|
| | | this.fname = fname;
|
| | | }
|
| | |
|
| | | public void setFile(File[] file) {
|
| | | this.file = file;
|
| | | }
|
| | |
|
| | | public void setFileFileName(String[] fileFileName) {
|
| | | this.fileFileName = fileFileName;
|
| | | }
|
| | |
|
| | |
|
| | | public void setFileContentType(String[] fileContentType) {
|
| | | this.fileContentType = fileContentType;
|
| | | }
|
| | |
|
| | | public String getResult() {
|
| | | return result;
|
| | | }
|
| | |
|
| | | public void setBattname(String battname) {
|
| | | this.battname = battname;
|
| | | } |
| | | |
| | | |
| | | |
| | | public void setJson(String json) {
|
| | | this.json = json;
|
| | | }
|
| | | |
| | | 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);*/
|
| | | }
|
| | | }
|
| | | package com.fgkj.actions; |
| | | |
| | | import java.awt.AlphaComposite; |
| | | import java.awt.Color; |
| | | import java.awt.Graphics2D; |
| | | import java.awt.RenderingHints; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import com.fgkj.dao.DAOHelper; |
| | | import com.fgkj.dao.UinfDaoFactory; |
| | | import com.fgkj.dto.BattInf; |
| | | import com.fgkj.dto.ImageSize; |
| | | import com.fgkj.dto.MyFile; |
| | | import com.fgkj.dto.ServiceModel; |
| | | import com.fgkj.dto.User_inf; |
| | | import com.fgkj.dto.User_log; |
| | | import com.fgkj.services.User_logService; |
| | | import com.google.gson.Gson; |
| | | |
| | | public class MyFileAction extends ActionUtil{ |
| | | |
| | | private String result; |
| | | private String json; |
| | | private String battname; |
| | | private File[] file; |
| | | // 提交过来的file的名字 |
| | | private String[] fileFileName; |
| | | // 提交过来的file的MIME类型 |
| | | private String[] fileContentType; |
| | | |
| | | private String filestr; //文件内容 |
| | | private String fname; //文件内容 |
| | | |
| | | //上传文件 |
| | | public String UploadTestFile(){ |
| | | 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(); |
| | | ServiceModel model = new ServiceModel(); |
| | | if(f){ |
| | | model.setCode(1); |
| | | model.setMsg("上传成功"); |
| | | }else{ |
| | | model.setCode(0); |
| | | model.setMsg("上传失败"); |
| | | } |
| | | result = ActionUtil.tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | public boolean createFile(){ |
| | | 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;i<file.length;i++){ |
| | | String savepath=root+battname+"/"+fileFileName[i]; |
| | | //System.out.println(savepath); |
| | | FileInputStream fis=null; |
| | | FileOutputStream fos=null; |
| | | try { |
| | | File f = new File(savepath); |
| | | if(!f.exists()){ |
| | | if(!f.getParentFile().exists()){ |
| | | f.getParentFile().mkdirs(); |
| | | } |
| | | f.createNewFile(); |
| | | } |
| | | /** 创建输入流 */ |
| | | fis = new FileInputStream(file[i]); |
| | | /** 输出流 根据文件的路径+文件名称创建文件对象 */ |
| | | fos = new FileOutputStream(f); |
| | | /** 读取字节 */ |
| | | byte b[] = new byte[1024]; |
| | | int n = 0; |
| | | /** 读取操作 */ |
| | | while ((n = fis.read(b)) != -1) { |
| | | /** 写操作 */ |
| | | fos.write(b, 0, n); |
| | | } |
| | | fos.flush(); |
| | | flag = true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally{ |
| | | /** 关闭操作 */ |
| | | try { |
| | | if (fis != null) { |
| | | fis.close(); |
| | | } |
| | | if (fos != null) { |
| | | fos.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | public String UploadIphoneFile(){ |
| | | 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"); |
| | | Gson gson = new Gson(); |
| | | int[] arr = gson.fromJson(filestr, int[].class); |
| | | |
| | | System.out.println("文件名"+fname); |
| | | System.out.println("文件内容:"+filestr); |
| | | System.out.println("电池组名称"+battname); |
| | | boolean f = createFileByarr(arr,fname,battname); |
| | | |
| | | ServiceModel model = new ServiceModel(); |
| | | if(f){ |
| | | model.setCode(1); |
| | | model.setMsg("上传成功"); |
| | | }else{ |
| | | model.setCode(0); |
| | | model.setMsg("上传失败"); |
| | | } |
| | | result = ActionUtil.tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param arr 文件的字节数组 |
| | | * @param fname 文件名 |
| | | * @param bname 电池组名称 |
| | | * @return 是否生成文件成功 |
| | | */ |
| | | public boolean createFileByarr(int[] arr,String fname,String bname){ |
| | | String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); |
| | | String str = new File(loadpath).getParentFile().getAbsolutePath(); |
| | | boolean flag = false; |
| | | String root = str+"/uploadfiles/"; // 上传路径 |
| | | //System.out.println(battname+"***"); |
| | | String savepath=root+bname+"/"+fname; |
| | | //System.out.println(savepath); |
| | | FileOutputStream fos=null; |
| | | byte[] bytes = new byte[arr.length]; |
| | | for(int i=0;i<arr.length;i++){ |
| | | bytes[i] = (byte)(arr[i] & 0xFF); |
| | | } |
| | | |
| | | try { |
| | | File f = new File(savepath); |
| | | if(!f.exists()){ |
| | | if(!f.getParentFile().exists()){ |
| | | f.getParentFile().mkdirs(); |
| | | } |
| | | f.createNewFile(); |
| | | } |
| | | fos = new FileOutputStream(savepath); |
| | | fos.write(bytes); |
| | | fos.flush(); |
| | | flag = true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally{ |
| | | /** 关闭操作 */ |
| | | try { |
| | | if (fos != null) { |
| | | fos.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | //上传机房视频接口 |
| | | public String uploadStationFile(){ |
| | | 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<file.length && binf != null;i++){ |
| | | String filePath = root+fileFileName[i]; |
| | | createFilefolderIFNotExist(filePath); |
| | | copyFile(file[i], filePath); |
| | | isSuccess = true; |
| | | } |
| | | if(isSuccess){ |
| | | model.setCode(1); |
| | | model.setMsg("上传成功"); |
| | | }else{ |
| | | model.setCode(0); |
| | | model.setMsg("上传失败"); |
| | | } |
| | | result = tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * 获取指定机房得资源 |
| | | */ |
| | | public String searchStationSource(){ |
| | | String fileRoot = "stationsrc"; //存放机房资源文件的文件夹 |
| | | BattInf binf = getGson().fromJson(json, BattInf.class); |
| | | ServiceModel model = new ServiceModel(); |
| | | String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); |
| | | String str = new File(loadpath).getParentFile().getAbsolutePath(); |
| | | if(binf != null && binf.getStationId() != null && binf.getVideoOrImage() != null){ |
| | | String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getVideoOrImage()+File.separator; //当前请求的机房所在的资源文件夹 |
| | | //System.out.println(filesource); |
| | | File source = new File(filesource); |
| | | File[] files = source.listFiles(); |
| | | model.setMsg("暂无视频文件"); |
| | | if(files!=null && files.length>0){ |
| | | List myfiles = new ArrayList<MyFile>(); |
| | | for(int i=0;i<files.length;i++){ |
| | | MyFile myfile = new MyFile(); |
| | | if(files[i].isFile()){ |
| | | myfile.setFile(files[i]); |
| | | if(binf.getVideoOrImage().equals("image")) { |
| | | myfile.setIms(ImageInfor(files[i])); |
| | | } |
| | | myfiles.add(myfile); |
| | | |
| | | } |
| | | } |
| | | if(myfiles.size() > 0){ |
| | | model.setCode(1); |
| | | model.setData(myfiles); |
| | | model.setMsg("读取成功"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | result = tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | //将文件复制到指定的目录 |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | //上传机房视频接口(手机端) |
| | | /*public String uploadStationFile_mobile(){ |
| | | 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<file.length && binf != null;i++){ |
| | | String filePath = root+fileFileName[i]; |
| | | createFilefolderIFNotExist(filePath); |
| | | if(binf.getVideoOrImage().equals("image")) { |
| | | GraphicsFile(binf.getBinfmation(), file[i], filePath); |
| | | }else { |
| | | copyFile(file[i], filePath); |
| | | } |
| | | isSuccess = true; |
| | | } |
| | | if(isSuccess){ |
| | | model.setCode(1); |
| | | model.setMsg("上传成功"); |
| | | }else{ |
| | | model.setCode(0); |
| | | model.setMsg("上传失败"); |
| | | } |
| | | result = tojson(model); |
| | | return SUCCESS; |
| | | }*/ |
| | | //将获取到得图片加水印 |
| | | /*public static void GraphicsFile(Object obj, File file,String newImagePath) { |
| | | //InputStream is = null; |
| | | OutputStream os = null; |
| | | BattMap_information binfmation=(BattMap_information) obj; |
| | | try { |
| | | // 1、源图片 |
| | | java.awt.Image srcImg = ImageIO.read(file); |
| | | //System.out.println(srcImg.); |
| | | BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); |
| | | // 2、得到画笔对象 |
| | | Graphics2D g = buffImg.createGraphics(); |
| | | // 3、设置对线段的锯齿状边缘处理 |
| | | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
| | | g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null); |
| | | // 4、设置水印旋转 |
| | | if (null != degree) { |
| | | //System.out.println("buffImg.getWidth():"+buffImg.getWidth()+" buffImg.getHeight():"+buffImg.getHeight()); |
| | | g.rotate(Math.toRadians(degree), buffImg.getWidth()/10,buffImg.getHeight() /10); |
| | | } |
| | | // 5、设置水印文字颜色 |
| | | g.setColor(new Color(255,0,0));//默认红色 |
| | | int font_size= buffImg.getHeight() /35; |
| | | // 6、设置水印文字Font |
| | | g.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD,font_size)); |
| | | // 7、设置水印文字透明度 |
| | | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.9f)); |
| | | // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(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_size<buffImg.getWidth()) { |
| | | g.drawString(address, 0 , font_size*(3)); |
| | | }else { |
| | | for (int i = 0; i <=address_size/buffImg.getWidth(); i++) { |
| | | if(i==0) { |
| | | g.drawString(address.substring( 0,buffImg.getWidth()/font_size*(i+1)), 0 , font_size*(2+i+1)); |
| | | }else { |
| | | if(i==address_size/buffImg.getWidth()) { |
| | | g.drawString(address.substring( buffImg.getWidth()/font_size*(i),(address.length()) ), 0 , font_size*(5+i+1)); |
| | | }else{ |
| | | g.drawString(address.substring( buffImg.getWidth()/font_size*(i),buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1)); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | //设置时间 |
| | | g.drawString("时间:"+DAOHelper.sdf.format(new Date()), 0 , font_size*4); |
| | | //设置机房id |
| | | g.drawString("机房ID:"+binfmation.getStationId(), 0 , font_size*5); |
| | | //设置备注 |
| | | String note="备注:"+binfmation.getStationName(); |
| | | int note_size=getTextLength(note)*font_size; |
| | | //System.out.println(getTextLength(binfmation.getStationName())); |
| | | //System.out.println("font_size:"+font_size+" note_size:"+note_size+" buffImg.getWidth():"+buffImg.getWidth()); |
| | | if(note_size<buffImg.getWidth()) { |
| | | g.drawString(note, 0 , font_size*6); |
| | | }else { |
| | | for (int i = 0; i <=note_size/buffImg.getWidth(); i++) { |
| | | if(i==0) { |
| | | g.drawString(note.substring( 0,buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1)); |
| | | }else { |
| | | if(i==note_size/buffImg.getWidth()) { |
| | | g.drawString(note.substring( buffImg.getWidth()/font_size*(i),(note.length()) ), 0 , font_size*(5+i+1)); |
| | | }else{ |
| | | g.drawString(note.substring( buffImg.getWidth()/font_size*(i),buffImg.getWidth()/font_size*(i+1) ), 0 , font_size*(5+i+1)); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 9、释放资源 |
| | | g.dispose(); |
| | | // 10、生成图片 |
| | | os = new FileOutputStream(newImagePath); |
| | | //System.out.println(os); |
| | | ImageIO.write(buffImg, "JPG", os); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (null != is) |
| | | is.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | try { |
| | | if (null != os) |
| | | os.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | }*/ |
| | | //计算水印文本长度 |
| | | //1、中文长度即文本长度 2、英文长度为文本长度二分之一 |
| | | public static int getTextLength(String text){ |
| | | //水印文字长度 |
| | | int length = text.length(); |
| | | |
| | | for (int i = 0; i < text.length(); i++) { |
| | | String s =String.valueOf(text.charAt(i)); |
| | | if (s.getBytes().length>1) { |
| | | 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 |
| | | */ |
| | | public String 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<File> allfiles = new ArrayList<File>(); |
| | | 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("暂无机房资源"); |
| | | } |
| | | result = tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * 删除指定的资源文件 |
| | | * @return |
| | | */ |
| | | public String deleteFile() { |
| | | 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)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("文件不存在"); |
| | | } |
| | | result = tojson(model); |
| | | return SUCCESS; |
| | | } |
| | | |
| | | |
| | | public void setFilestr(String filestr) { |
| | | this.filestr = filestr; |
| | | } |
| | | |
| | | public void setFname(String fname) { |
| | | this.fname = fname; |
| | | } |
| | | |
| | | public void setFile(File[] file) { |
| | | this.file = file; |
| | | } |
| | | |
| | | public void setFileFileName(String[] fileFileName) { |
| | | this.fileFileName = fileFileName; |
| | | } |
| | | |
| | | |
| | | public void setFileContentType(String[] fileContentType) { |
| | | this.fileContentType = fileContentType; |
| | | } |
| | | |
| | | public String getResult() { |
| | | return result; |
| | | } |
| | | |
| | | public void setBattname(String battname) { |
| | | this.battname = battname; |
| | | } |
| | | |
| | | |
| | | |
| | | public void setJson(String json) { |
| | | this.json = json; |
| | | } |
| | | |
| | | 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);*/ |
| | | } |
| | | } |