From d42cb45f8fc0932e9a0766f98c445635a85b22d3 Mon Sep 17 00:00:00 2001 From: admin:huo123456@qq <496960745@qq.com> Date: 星期一, 21 一月 2019 09:15:22 +0800 Subject: [PATCH] 修改 --- gx_tieta/src/com/fgkj/actions/MyFileAction.java | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 230 insertions(+), 7 deletions(-) diff --git a/gx_tieta/src/com/fgkj/actions/MyFileAction.java b/gx_tieta/src/com/fgkj/actions/MyFileAction.java index 8bc9101..8b74961 100644 --- a/gx_tieta/src/com/fgkj/actions/MyFileAction.java +++ b/gx_tieta/src/com/fgkj/actions/MyFileAction.java @@ -1,14 +1,28 @@ 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; @@ -175,22 +189,67 @@ //涓婁紶鏈烘埧瑙嗛鎺ュ彛 public String uploadStationFile(){ BattInf binf = getGson().fromJson(json, BattInf.class); - String fileRoot = "staionsrc"; + 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()+"/vide/"; // 涓婁紶璺緞 - for(int i=0;i<file.length;i++){ + 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]); + 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; @@ -225,8 +284,154 @@ } } } + + //涓婁紶鏈烘埧瑙嗛鎺ュ彛(鎵嬫満绔�) + 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銆佽缃按鍗版枃瀛桭ont + 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; } @@ -258,8 +463,26 @@ + public void setJson(String json) { + this.json = json; + } + public static void main(String[] args) { - String filePath = "D:/test/a/a.txt"; - ActionUtil.createFileRootIFNotExist(filePath); + /*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鐧捐壊涔愪笟鍘跨珛鏂板皬鍖篧F-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); } } -- Gitblit v1.9.1