- 浏览: 442563 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (192)
- spring (4)
- jFreeChart (1)
- oracle (3)
- tomcat (1)
- mysql (2)
- jbpm (2)
- jQuery (4)
- 财经 (3)
- php (1)
- linux (1)
- ExtJs (2)
- android (92)
- 生活 (5)
- Json (2)
- html (4)
- java (24)
- other (16)
- struts 2 (3)
- hibernate (3)
- Lucene (2)
- Thread (1)
- javaScript (2)
- weblogic (1)
- T-SQL (3)
- Ant (1)
- memcached (6)
- apache (1)
- android 事件处理 (0)
- git (1)
- 设计模式 (1)
最新评论
-
feng_tai_jun:
...
weblogic.xml 部署描述符元素 -
hnraysir:
赞,必须要顶顶顶!
Android中的Surface和SurfaceView -
hety163:
如果你设置的是一个viewgroup的ontouch,想判断不 ...
一个view如何同时响应onTouch和onClick事件 -
yue_670176656:
[flash=200,200][/flash][img][ur ...
九宫格手势密码案例 -
asdf_2012:
必须顶,好文章。简洁
Android中的Surface和SurfaceView
package com.cmmb.mall.service;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Service;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 处理压缩图片
* @param url 图片原地址
* @param downUrl 下载到本地图片地址
* @param productName 商品名称
* @param tempUrl 存放图片临时文件地址
* @param picImageUrl 处理之后的图片地址
* @param pcModel 机型
* @return 处理后的图片地址,用于FTP上传
*/
public String[] useImageDiapose(String url,String downUrl,String productName,String tempUrl,String picImageUrl,String pcModel){
String httpUrl[] = new String[2];
try {
ImageToolDiapose mypic = new ImageToolDiapose();
/*File picFileL = new File(picImageUrl+"L/");
if(!picFileL.exists()){
picFileL.mkdir();
}
File picFileM = new File(picImageUrl+"M/");
if(!picFileM.exists()){
picFileM.mkdir();
}*/
String filePath = mypic.saveUrlAs(url, downUrl);
System.out.println(filePath);
File file = new File(filePath);
String[] imgList = file.list();
for (int i = 0; i < imgList.length; i++) {
//文件名
String fileName = productName;
fileName += imgList[i].substring(imgList[i].lastIndexOf("."));
//copy到指定临时路径,对文件重命名
mypic.copyFile(filePath+"/"+imgList[i], tempUrl, fileName);
if(pcModel.equals("240x320")){
//机型1
//中图
mypic.compressPic(tempUrl,picImageUrl+"240x320/M/", fileName,fileName,125, 125, false);
httpUrl[0] = picImageUrl+"240x320/M/"+fileName;
//大图
mypic.compressPic(tempUrl,picImageUrl+"240x320/L/", fileName,fileName,240, 240, false);
httpUrl[1] = picImageUrl+"240x320/L/"+fileName;
}
if(pcModel.equals("320x480")){
//机型2
//中图
mypic.compressPic(tempUrl,picImageUrl+"320x480/M/", fileName,fileName,195, 195, false);
httpUrl[0] = picImageUrl+"320x480/M/"+fileName;
//大图
mypic.compressPic(tempUrl,picImageUrl+"320x480/L/", fileName,fileName,320, 320, false);
httpUrl[1] = picImageUrl+"320x480/L/"+fileName;
}
if(pcModel.equals("480x800")){
//机型3
//中图
mypic.compressPic(tempUrl,picImageUrl+"480x800/M/", fileName,fileName,290, 290, false);
httpUrl[0] = picImageUrl+"480x800/M/"+fileName;
//大图
mypic.compressPic(tempUrl,picImageUrl+"480x800/L/", fileName,fileName,480, 480, false);
httpUrl[1] = picImageUrl+"480x800/L/"+fileName;
}
if(pcModel.equals("540x960")){
//机型4
//中图
mypic.compressPic(tempUrl,picImageUrl+"540x960/M/", fileName,fileName,330, 330, false);
httpUrl[0] = picImageUrl+"540x960/M/"+fileName;
//大图
mypic.compressPic(tempUrl,picImageUrl+"540x960/L/", fileName,fileName,540, 540, false);
httpUrl[1] = picImageUrl+"540x960/L/"+fileName;
}
if(pcModel.equals("640x960")){
//机型4
//中图
mypic.compressPic(tempUrl,picImageUrl+"640x960/M/", fileName,fileName,390, 390, false);
httpUrl[0] = picImageUrl+"640x960/M/"+fileName;
//大图
mypic.compressPic(tempUrl,picImageUrl+"640x960/L/", fileName,fileName,640, 640, false);
httpUrl[1] = picImageUrl+"640x960/L/"+fileName;
}
//删除文件
mypic.deleteFile(tempUrl+fileName);
mypic.deleteFile(filePath+"/"+imgList[i]);
//System.out.println("处理图片成功---------------");
}
} catch (Exception e) {
System.out.println("处理图片失败----------------");
e.printStackTrace();
}
return httpUrl;
}
// 图片处理
public String compressPic() {
try {
//获得源文件
file = new File(inputDir + inputFileName);
if (!file.exists()) {
return "";
}
Image img = ImageIO.read(file);
// 判断图片格式是否正确
if (img.getWidth(null) == -1) {
System.out.println(" can't read,retry!" + "<BR>");
return "no";
} else {
int newWidth; int newHeight;
// 判断是否是等比缩放
if (this.proportion == true) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
// 根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outputWidth; // 输出的图片宽度
newHeight = outputHeight; // 输出的图片高度
}
BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
* 优先级比速度高 生成的图片质量比较好 但速度慢
*/
tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
// JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//System.out.println("执行压缩质量==");
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(0.85f, false); //设置压缩质量
encoder.setJPEGEncodeParam(param);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return "ok";
}
/**
* FTP上传
* @param dir 资源目录
* @param ftpip ip地址
* @param port 端口号
* @param ftpuser 用户名
* @param ftppassword 密码
* @param path 要上传到的目录
* @return
*/
public boolean ftpUpload(String dir, String ftpip, int port,
String ftpuser, String ftppassword,String path) {
System.out.println("================FTPINFOMATION======================"
+ "dir-" + dir + "ftpip-" + ftpip + "port-" + port
+ "ftpuser-" + ftpuser + "ftppasswd-" + ftppassword);
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
File srcdir = new File(dir);
boolean a = false;
String ftp_model = "1";
try {
System.out.println(dir + "===========dir===========");
//与FTP建立连接
ftpClient.connect(ftpip, port);
if (!"".equals(ftp_model) && ftp_model.equals("1")) {
// 新添加 ( 被动模式 ),程序默认为主动
ftpClient.enterLocalPassiveMode();
}
//设置登录名密码
ftpClient.login(ftpuser, ftppassword);
// 设置上传目录,(Ftp用戶的目录)
ftpClient.changeWorkingDirectory(path);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
if (srcdir.isDirectory()) {// 如果是目录
File files[] = srcdir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
fis = new FileInputStream(files[i]);
ftpClient.storeFile(files[i].getName(), fis);
}
}
} else if (srcdir.isFile()) {// 如果是文件
fis = new FileInputStream(srcdir);
System.out.println("==========storeName========="+srcdir.getName());
System.out.println("==========storeName========="+new String(srcdir.getName().getBytes("GBK"),"iso-8859-1"));
ftpClient.storeFile(new String(srcdir.getName().getBytes("GBK"),"iso-8859-1"), fis);
}
a = true;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return a;
}
/**
* 下载远程文件
* @param photoUrl 文件路径
* @param localPath 下载到本地的路径
* @return 是否成功
*/
public String saveUrlAs(String photoUrl, String localPath) {
String filePath = null;
try {
File file = new File(localPath);
if(!file.exists()){
file.mkdir();
}
//临时文件名
String dowmFileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String tempFileName = file+dowmFileName;
filePath = file.toString();
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(tempFileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
return filePath;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
/**
* 获取远程文件的内容(兼容HTTP与FTP)
* @param urlString 文件路径
* @return 文件内容
*/
public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "\n");
}
reader.close();
} catch (MalformedURLException e) {
System.out.println("Unable to connect to URL: " + urlString);
} catch (IOException e) {
System.out.println("IOException when connecting to URL: " + urlString);
}
return document.toString();
}
// main测试
// compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
public static void main(String[] arg) {
ImageToolDiapose imageTool = new ImageToolDiapose();
Properties props = new Properties();
//读取配置文件
String urlpath = ImageToolDiapose.class.getClassLoader().getResource(("util.properties")).toString().substring(6);
String empUrl = urlpath.replace("%20", " ");
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(empUrl));
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(props.get("ftpIp")+"="+props.getProperty("ftpPwd"));
/* Properties p = new Properties();
try {
InputStream inStream = new FileInputStream(new File("/util.properties"));
p.load(inStream);
} catch (Exception e) {
e.printStackTrace();
}
System.err.println(p.get("ftpUser"));*/
String url1[] = imageTool.useImageDiapose("http://img02.taobaocdn.com/bao/uploaded/i2/T1ZH6kXdtXXXacfj.9_103917.jpg","d:/img","806","c:/img/", "e:/image/","320x480");
imageTool.ftpUpload(url1[0], "192.168.1.117", 21,"pic", "123456","/320X480/M");
imageTool.ftpUpload(url1[1], "192.168.1.117", 21,"pic", "123456","/320X480/L");
//String url2[] = imageTool.useImageDiapose("http://img02.taobaocdn.com/bao/uploaded/i2/T16cfiXixbXXXPghU6_061818.jpg","d:/img","中华人民","c:/img/", "e:/image/","640x960");
//imageTool.ftpUpload(url2[0], "192.168.1.117", 21,"pic", "123456","/640X960/M");
//imageTool.ftpUpload(url2[1], "192.168.1.117", 21,"pic", "123456","/640X960/L");
/*String photoUrl = "http://img01.taobaocdn.com/bao/uploaded/i1/T1UltYXhVbXXbIbiAZ_032637.jpg";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
System.out.println(fileName);
String filePath = "D:/img";*/
// System.out.println(ImageToolDiapose.getDocumentAt(photoUrl));
//ImageToolDiapose.saveUrlAs(photoUrl, filePath + fileName);
// System.out.println("Run ok!\n Get URL file " + flag);
}
}
--------------------------------------------------------------------------------------------------------------
本实例流程:1、下载远程文件图片到本地
2、对图片处理(按照不同大小)
3、将处理好的图片用FTP方式上传到指定目录
发表评论
-
Java中文件与字节数组转换
2015-01-09 14:19 6151Java文件与字节数组转换 /** * 返回一 ... -
Eclipse中的快速Java代码模板使用 java editor template
2014-10-31 11:11 4967java editor template Eclipse ... -
java回调函数
2014-06-29 14:55 1107java回调函数 所谓回调,就是客户程序C调用 ... -
java中double型数据四舍五入
2014-03-14 19:38 1284java中double型数据四舍五入 /** * ... -
Java线程池介绍以及简单示例
2014-01-23 17:34 1073Java线程池介绍以及简 ... -
eclipse中jar包关联源代码与取消关联
2014-01-13 11:55 2654在eclipse中查看Android源代码 当我们 ... -
Java解读NIO Socket非阻塞模式
2013-11-01 13:48 868Java解读NIO Socket非阻塞模式 Jav ... -
设计模式之java接口和java抽象类
2013-10-09 09:05 1255java接口和java抽象类 java不允许多重 ... -
Java学习之路:不走弯路,就是捷径
2013-04-13 18:37 954Java学习之路:不走弯路,就是捷径 1.如何学习 ... -
java.util.concurrent.ExecutorService与Executors例子的简单剖析
2013-03-25 15:11 1824java.util.concurrent.ExecutorS ... -
Socket
2012-12-23 17:30 1058Socket 定义: 两个java应用程序可通过一个 ... -
jdom读取xml文件
2012-08-19 19:53 1210用JDOM读取XML文件需先 ... -
java 获取HTTP 头部信息
2012-07-21 19:16 2293java 获取HTTP 头部信息 public c ... -
java读取properties配置文件
2012-06-12 11:25 1172public static void main(Stri ... -
使用HTTP方式下载文件( 仅支持另存为)
2012-04-20 18:07 2585public class DownFile extend ... -
java实现FTP断点续传
2012-04-13 09:05 2542import java.io.File; i ... -
java 读取文件
2012-04-12 20:39 1048读取:将磁盘文件数据 ... -
java操作Excel文件
2012-04-06 11:56 1190JAVA EXCEL API 简介 Java Exce ... -
java 调用视频转换工具ffmpeg
2012-03-28 20:41 1705import java.io.File; impor ... -
JAVA对存储过程的调用方法
2012-03-01 11:55 4067一:Java如何实现对存 ...
相关推荐
FTP(File Transfer Protocol)是一种广泛使用的网络协议,用于在互联网上进行文件传输。...总的来说,这个实例展示了如何使用VS2017和FTP协议,结合Base64编码技术,实现在程序中高效地下载和处理远程图片。
您还可以用它连接到您的Windows PC或是使用FTP,SFTP,WebDAV协议的远程服务器。顺便一提,您还能用它创建或提取最常用的压缩格式,如ZIP,RAR和7ZIP。 您或许也已经厌倦了界面老旧的root文件管理器?那么此文件...
X-plore可以列出Picasa相册中的图片,下载它们,创建相册,上传和删除照片,编辑字幕。 SQLite数据库查看器 X-plore可以将SQLite数据库文件(具有.db扩展名的文件)显示为可扩展的表列表,每个表包含具有数据库条目...
的X PLORE可以在Picasa相册列表图片,下载,创建相册,上传和删除照片,编辑字幕。 SQLite数据库查看器 的X PLORE可以显示SQLite数据库文件(那些。db扩展名)作为表的扩展列表中,用数据库条目的行和列的每个表包含...
在网站部署时,DOS命令可结合FTP或SCP命令上传文件到服务器,或者配合Git进行版本控制操作。例如,`git clone`用于克隆远程仓库,`git pull`更新本地代码,`git push`则将本地更改推送到服务器。 综上所述,网站...
在备份和恢复配置上,思科通常使用`copy running-config tftp`或`copy startup-config tftp`命令,而华为则使用`save`命令保存配置,`copy running-config scp`或`copy running-config ftp`来上传配置到远程服务器。...
1350多个精品易语言模块提供下载介绍 1亦思验证码识别1.5免费版.ec 24位转单色位图模块.ec 32张发牌.ec 3D引擎支持库-eOgre.ec 69msn.ec ACCESS 到高级表格.ec Access操作.ec Access数据库压缩修复新建.ec ACCSEE...
ftp文件操作模块.ec gdiplus类模块.ec GetStringSize.ec GIF快 照.ec Hex-Dec.ec Hex-Dec1.ec Hex2Dec.ec hide.ec hotkey.ec HTTP.ec http_ec.ec http_ec1.ec HTTP 访问模块 .ec HTTP访问模块1.0.ec HTTP访问模块1.1...