标签:directory try 文件夹 pat span color pre iss client
saveImage()方法
// 分别获取当前日期 与小时做文件夹 SimpleDateFormat formapath = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat formahhpath = new SimpleDateFormat("HH"); // 分别获取当前日期 与小时做文件夹 String yyMMdd = formapath.format(new Date()); String hours = formahhpath.format(new Date()); // 文件名 String fileName = "123456.jpg"; // 文件夹路径 String filePath = yyMMdd + "/" + hours; // 上传的base64字符串 String imgbyte = json.getString("IMGBYTE"); imgbyte = imgbyte.replace(" ", "+"); byte[] inputByte = Base64.decodeBase64(imgbyte); InputStream insComing = new ByteArrayInputStream(inputByte); boolean isSuccess = upLoadFile("172.60.6.51", 21, "ftpuser", "ftpuser1", filePath, fileName, insComing); if (!isSuccess) throw new ServiceException("", "保存进FTP服务器失败");
upLoadFile方法
private boolean upLoadFile(String IP, int port, String userName, String password, String pathName, String fileName, InputStream in) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(IP, port);// 连接FTP服务器 // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftp.login(userName, password);// 登录 ftp.setDataTimeout(30000); ftp.setBufferSize(1024); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); throw new IOException("FTP登录失败" + IP + "/" + port + "/" + userName + "/" + password); } for (String str : pathName.split("/")) { ftp.makeDirectory(str); ftp.changeWorkingDirectory(str); } boolean tag = ftp.storeFile(fileName, in); if (!tag) { throw new Exception("保存到FTP失败"); } success = true; } catch (Exception e) { try { throw new Exception("图片上传FTP失败" + e.getMessage()); } catch (Exception e1) { e1.printStackTrace(); } } finally { try { in.close();// 在这里关闭,保证一定要关闭 ftp.logout();// 在这里登出,保证一定要登出 } catch (IOException e) { e.printStackTrace(); } if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
附上base64与图片转换的在线转换地址:http://tool.chinaz.com/tools/imgtobase
java中把base64格式的字符串转化为图片并保存到ftp服务器上
标签:directory try 文件夹 pat span color pre iss client
原文地址:https://www.cnblogs.com/masha2017/p/14022789.html