51,397
社区成员




public void uploadFtpServer(String filePath, String fileName, String ftpPath) {
logger.info(
"uploadFtpServer(String filePath, String fileName, String ftpPath={}) --start",
filePath, fileName, ftpPath);
FTPClient client = new FTPClient();
int reply = 0;
try {
client.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("zh");
client.connect(url, port);
client.login(username, password);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
} else {
if (!client.changeWorkingDirectory(ftpPath)) {
client.makeDirectory(ftpPath);
client.changeWorkingDirectory(ftpPath);
}
client.enterLocalPassiveMode();
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.setBufferSize(1024 * 1024 * 10);
//client.changeWorkingDirectory(filePath);
//InputStream input = new FileInputStream(new File(filePath));
BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File(filePath)));
// InputStreamReader input = new InputStreamReader(new FileInputStream(new File(filePath)));
client.storeFile(fileName, input);
input.close();
client.logout();
}
} catch (Exception e) {
throw new BaseException(ExceptionCategory.Illegal_Parameter,
"将上传到服务器中的文件读取出来存入ftp文件服务器时出现错误!" + e.getMessage());
} finally {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException ioe) {
}
}
logger.info(
"uploadFtpServer(String filePath, String fileName, String ftpPath={}) --end",
filePath, fileName, ftpPath);
}
}
public String zipImageFile(File oldFile, File newFile, float rate) {
if (oldFile == null) {
return null;
}
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
int h = srcFile.getHeight(null);
int height = (int) (h * rate);
int width = (int) (w * rate);
String srcImgPath = newFile.getAbsoluteFile().toString();
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1,
srcImgPath.length());
BufferedImage buffImg = null;
if (subfix.equals("png")) {
buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
} else {
buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
}
Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height,
Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(buffImg, subfix, new File(srcImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}