等比例压缩图片上传到FTP。有时候会变成黑白图片,是怎么回事?

夜空霓虹 2018-07-06 05:43:17
上传到FTP


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();
}



...全文
225 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
夜空霓虹 2018-07-06
  • 打赏
  • 举报
回复
而且是不定时出现的。

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧