admin:huo123456@qq
2019-01-21 d42cb45f8fc0932e9a0766f98c445635a85b22d3
gx_tieta/src/com/fgkj/actions/MyFileAction.java
@@ -1,16 +1,27 @@
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.dto.BattInf;
import com.fgkj.dto.BattMap_information;
import com.fgkj.dto.MyFile;
import com.fgkj.dto.ServiceModel;
import com.google.gson.Gson;
@@ -183,7 +194,7 @@
      boolean isSuccess = false;
      String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
      String str = new File(loadpath).getParentFile().getAbsolutePath();                                       //获取服务器所在的绝对路径
      String root = str+"/"+fileRoot+"/"+binf.getStationId()+"/video/";                                       // 上传路径
      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);
@@ -211,8 +222,8 @@
      ServiceModel model = new ServiceModel();
      String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
      String str = new File(loadpath).getParentFile().getAbsolutePath();   
      if(binf != null && binf.getStationId() != null && binf.getStationName() != null){
         String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getStationName()+File.separator;            //当前请求的机房所在的资源文件夹
      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();
@@ -274,8 +285,153 @@
      }
   }
   
   //上传机房视频接口(手机端)
   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 void setFilestr(String filestr) {
      this.filestr = filestr;
   }
@@ -312,11 +468,21 @@
   }
   public static void main(String[] args) {
      String filePath = "D:/test/a/a.txt";
      /*String filePath = "D:/test/a/a.txt";
      //ActionUtil.createFileRootIFNotExist(filePath);
      
      File file = new File(filePath);
      System.out.println(ActionUtil.tojson(file));
      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);
   }
}