java 拷贝文件到服务器_Java自动从服务器复制文件并且运行可执行文件并打包

1 packagecom.prize;2

3 importjava.io.BufferedInputStream;4 importjava.io.BufferedOutputStream;5 importjava.io.File;6 importjava.io.FileFilter;7 importjava.io.FileInputStream;8 importjava.io.FileNotFoundException;9 importjava.io.FileOutputStream;10 importjava.io.IOException;11 importjava.io.InputStreamReader;12 importjava.io.RandomAccessFile;13 importjava.io.Reader;14 importjava.nio.ByteBuffer;15 importjava.nio.channels.FileChannel;16 importjava.util.zip.ZipEntry;17 importjava.util.zip.ZipOutputStream;18

19 public classCopyPackaging {20 public static boolean APDB1 = false;21 public static boolean APDB2 = false;22 public static boolean BPLGU1 = false;23 public static boolean BPLGU2 = false;24 public static boolean SERVERADDRESSRIGHTWRONG = false;25 public static String OUTADDRESS = "\\out\\target\\product\\prize6735m_65c_l1";26 public static String APDBADDRESS = "\\out\\target\\product\\prize6735m_65c_l1\\obj\\CGEN\\";27 public static String APDBFIRST = "APDB_MT6735_S01_L1.MP3_W15.29";28 public static String APDBSECOND = "APDB_MT6735_S01_L1.MP3_W15.29_ENUM";29 public static String BPLGUADDRESS = "\\out\\target\\product\\prize6735m_65c_l1\\system\\etc\\mddb\\";30 public static String BPLGUFIRST = "BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_ltg_n";31 public static String BPLGUSECOND = "BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_lwg_n";32 public static String PACKAGINGNAME = "";33

34 public static voidmain(String args[]) {35 try{36 serverCopyToLocation("Z:\\dux_pcba-os", "D:\\");37 } catch(IOException e) {38 e.printStackTrace();39 }40 }41

42 //判断服务器地址正确与否

43 public static booleanjudgeServerAddress(String serverAddress) {44 File sf = newFile(serverAddress);45 int num = sf.listFiles(newFileFilter() {46

47 @Override48 public booleanaccept(File pathname) {49 if (pathname.isDirectory() && pathname.getName().equals("out")) {50 return true;51 }52 return false;53 }54

55 }).length;56 if (num == 1) {57 return true;58 }59 return false;60 }61

62 //获取包名

63 public static voidgetPackingName(String serverAddress) {64 File sf = new File(serverAddress + OUTADDRESS + "\\system\\build.prop");65 StringBuffer name = newStringBuffer();66 Reader reader = null;67

68 try{69 reader = new InputStreamReader(newFileInputStream(sf));70 inttempchar;71 while ((tempchar = reader.read()) != -1) {72

73 name.append((char) tempchar);74

75 }76 PACKAGINGNAME =name.toString()77 .substring(name.toString().indexOf("ro.build.display.id="),78 name.toString().indexOf("ro.build.version.incremental"))79 .substring(name.toString()80 .substring(name.toString().indexOf("ro.build.display.id=") - 1,81 name.toString().indexOf("ro.build.version.incremental"))82 .indexOf("="))83 .toString().replace("\n", "");84 } catch(FileNotFoundException e) {85 e.printStackTrace();86 } catch(IOException e) {87 e.printStackTrace();88 } finally{89 if (reader != null) {90 try{91 reader.close();92 } catch(IOException e) {93 //TODO Auto-generated catch block

94 e.printStackTrace();95 }96 }97 }98 }99

100 //从服务器拷贝文件到系统盘符

101 public static void serverCopyToLocation(String serverAddress, String locationAddress) throwsIOException {102 File sf = new File(serverAddress +OUTADDRESS);103 String locationTarget = locationAddress + "packaging";104 File nlf = newFile(locationTarget);105 if (!nlf.exists() && !nlf.isDirectory()) {106 nlf.mkdir();107 }108 getPackingName(serverAddress);109 File[] allSource =sf.listFiles();110 for (int i = 0; i < allSource.length; i++) {111 if (!allSource[i].isDirectory()) {112 System.out.println("path=" + allSource[i].getAbsolutePath() + "--------" +allSource[i].getName());113 nioTransferCopy(newFile(allSource[i].getAbsolutePath()),114 new File(locationTarget + "\\" +allSource[i].getName()));115 }116 }117

118 judgeServerOutNeedCopy(serverAddress);119 if (!APDB1 && new File(serverAddress + APDBADDRESS +APDBFIRST).exists()) {120 nioTransferCopy(new File(serverAddress + APDBADDRESS +APDBFIRST),121 new File(locationTarget + "\\" +APDBFIRST));122 }123 if (!APDB2 && new File(serverAddress + APDBADDRESS +APDBSECOND).exists()) {124 nioTransferCopy(new File(serverAddress + APDBADDRESS +APDBSECOND),125 new File(locationTarget + "\\" +APDBSECOND));126 }127 if (!BPLGU1 && new File(serverAddress + BPLGUADDRESS +BPLGUFIRST).exists()) {128 nioTransferCopy(new File(serverAddress + BPLGUADDRESS +BPLGUFIRST),129 new File(locationTarget + "\\" +BPLGUFIRST));130 }131 if (!BPLGU2 && new File(serverAddress + BPLGUADDRESS +BPLGUSECOND).exists()) {132 nioTransferCopy(new File(serverAddress + BPLGUADDRESS +BPLGUSECOND),133 new File(locationTarget + "\\" +BPLGUSECOND));134 }135

136 nioTransferCopy(new File(System.getProperty("user.dir") + "\\" + "CheckSum_Gen.exe"),137 new File(locationTarget + "\\" + "CheckSum_Gen.exe"));138

139 nioTransferCopy(new File(System.getProperty("user.dir") + "\\" + "FlashToolLib.dll"),140 new File(locationTarget + "\\" + "FlashToolLib.dll"));141

142 System.getProperty("user.dir");143 if (checkSum(System.getProperty("user.dir") + "\\" + "CheckSum_Gen.exe", locationTarget)) {144 try{145 Thread.sleep(20000);146 } catch(InterruptedException e) {147 //TODO Auto-generated catch block

148 e.printStackTrace();149 }150 deleteFile(locationTarget);151 fileToZip(locationTarget, "D:\\", PACKAGINGNAME);152 }153 }154

155 //单线程复制文件最快方法,文件越大越有优势

156 public static voidnioTransferCopy(File source, File target) {157 FileChannel fc = null;158 FileChannel fco = null;159 try{160 fc = newFileInputStream(source).getChannel();161 fco = new RandomAccessFile(target, "rw").getChannel();162

163 ByteBuffer buf = ByteBuffer.allocate(1024);164

165 while (fc.read(buf) != -1) {166 buf.flip();167 fco.write(buf);168 buf.clear();169 }170 } catch(FileNotFoundException e) {171 //TODO Auto-generated catch block

172 e.printStackTrace();173 } catch(IOException e) {174 //TODO Auto-generated catch block

175 e.printStackTrace();176 } finally{177 try{178 fc.close();179 fco.close();180 } catch(IOException e) {181 //TODO Auto-generated catch block

182 e.printStackTrace();183 }184

185 }186

187 }188

189 /**

190 * 判断是否需要负责那4个文件191 *192 *@return

193 */

194 public static voidjudgeServerOutNeedCopy(String serverAddress) {195 System.out.println(serverAddress);196 File f = new File(serverAddress +OUTADDRESS);197 File[] fPath =f.listFiles();198 for (int i = 0; i < fPath.length; i++) {199

200 if (fPath[i].getName().equals("APDB_MT6735_S01_L1.MP3_W15.29")) {201 APDB1 = true;202 }203 if (fPath[i].getName().equals("APDB_MT6735_S01_L1.MP3_W15.29_ENUM")) {204 APDB2 = true;205 }206 if(fPath[i].getName()207 .equals("BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_ltg_n")) {208 BPLGU1 = true;209 }210 if(fPath[i].getName()211 .equals("BPLGUInfoCustomAppSrcP_MT6735_S00_MOLY_LR9_W1444_MD_LWTG_CMCC_MP_V10_P5_1_lwg_n")) {212 BPLGU2 = true;213 }214

215 }216 }217

218 /**

219 * 执行CheckSum_Gen程序220 *221 *@paramchecksum222 * :可执行文件路径223 *@paramtarget224 * :生成的文件存放路径225 *@return

226 */

227 public static booleancheckSum(String checksum, String target) {228 try{229

230 String[] ss = { "cmd.exe", "/k", "start", checksum };231 Runtime.getRuntime().exec(ss, null, newFile(target));232

233 } catch(Exception e) {234 e.printStackTrace();235 System.out.println(e.toString());236 return false;237 }238 return true;239 }240

241 /**

242 * 删除多余的文件243 *244 *@return

245 */

246 public static voiddeleteFile(String target) {247 File file = newFile(target);248 File[] showFile =file.listFiles();249 for (int i = 0; i < showFile.length; i++) {250 if (showFile[i].getName().equals("android-info.txt") || showFile[i].getName().equals("appsboot.mbn")251 || showFile[i].getName().equals("appsboot.raw") || showFile[i].getName().equals("appsboot.raw")252 || showFile[i].getName().equals("boot-verified.img")253 || showFile[i].getName().equals("CheckSum_Gen.exe")254 || showFile[i].getName().equals("clean_steps.mk")255 || showFile[i].getName().equals("custom_build_verno")256 || showFile[i].getName().equals("installed-files.txt") || showFile[i].getName().equals("kernel")257 || showFile[i].getName().equals("lk-verified.bin")258 || showFile[i].getName().equals("logo-verified.bin")259 || showFile[i].getName().equals("previous_build_config.mk")260 || showFile[i].getName().equals("ramdisk.img")261 || showFile[i].getName().equals("ramdisk-recovery.img")262 || showFile[i].getName().equals("recovery-verified.img")263 || showFile[i].getName().equals("secro-verified.img")264 || showFile[i].getName().equals("FlashToolLib.dll")265 || showFile[i].getName().equals("target_files-package.zip")) {266 showFile[i].delete();267

268 }269 }270 }271

272 //打包

273 /**

274 * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下275 *276 *@paramsourceFilePath277 * :待压缩的文件路径278 *@paramzipFilePath279 * :压缩后存放路径280 *@paramfileName281 * :压缩后文件的名称282 *@return

283 */

284 public static booleanfileToZip(String sourceFilePath, String zipFilePath, String fileName) {285 boolean flag = false;286 File sourceFile = newFile(sourceFilePath);287 FileInputStream fis = null;288 BufferedInputStream bis = null;289 FileOutputStream fos = null;290 ZipOutputStream zos = null;291

292 if (sourceFile.exists() == false) {293 System.out.println("待压缩的文件目录:" + sourceFilePath + "不存在.");294 } else{295 try{296 File zipFile = new File(zipFilePath + "/" + fileName + ".zip");297 if(zipFile.exists()) {298 System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");299 } else{300 File[] sourceFiles =sourceFile.listFiles();301 if (null == sourceFiles || sourceFiles.length < 1) {302 System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");303 } else{304 fos = newFileOutputStream(zipFile);305 zos = new ZipOutputStream(newBufferedOutputStream(fos));306 byte[] bufs = new byte[1024 * 10];307 for (int i = 0; i < sourceFiles.length; i++) {308 //创建ZIP实体,并添加进压缩包

309 ZipEntry zipEntry = newZipEntry(sourceFiles[i].getName());310 zos.putNextEntry(zipEntry);311 //读取待压缩的文件并写进压缩包里

312 fis = newFileInputStream(sourceFiles[i]);313 bis = new BufferedInputStream(fis, 1024 * 10);314 int read = 0;315 while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {316 zos.write(bufs, 0, read);317 }318 }319 flag = true;320 }321 }322 } catch(FileNotFoundException e) {323 e.printStackTrace();324 throw newRuntimeException(e);325 } catch(IOException e) {326 e.printStackTrace();327 throw newRuntimeException(e);328 } finally{329 //关闭流

330 try{331 if (null !=bis)332 bis.close();333 if (null !=zos)334 zos.close();335 } catch(IOException e) {336 e.printStackTrace();337 throw newRuntimeException(e);338 }339 }340 }341 returnflag;342 }343 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值