网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码实现图片拷贝 java图片上传代码

一个关于JAVA拷贝图片的问题

import java.io.*;

创新互联公司专注于企业网络营销推广、网站重做改版、六合网站定制设计、自适应品牌网站建设、H5高端网站建设商城网站定制开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为六合等各大城市提供网站开发制作服务。

public class BisicImageCopy {

public static void main(String[] args) {

FileInputStream fin = null;

FileOutputStream fout = null;

try {

fin = new FileInputStream("e:\\Car.jpg");

fout = new FileOutputStream("d:\\Car.jpg");

byte[] b = new byte[512];

int n;

while((n = fin.read(b))!=-1){

fout.write(b,0,n);//这里改改

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

fin.close();

fout.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

用Java编写一个程序,将一个图像文件复制到指定的文件夹中

这是我们公司基类里的一个方法希望对你有帮助。。/**

* 复制单个文件

* @param oldPath String 原文件路径 如:c:/fqf.txt

* @param newPath String 复制后路径 如:f:/fqf.txt

* @return boolean

*/

public void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) { //文件存在时

InputStream inStream = new FileInputStream(oldPath); //读入原文件

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

int length;

while ( (byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; //字节数 文件大小

// System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

}

}

catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace(); } }

JAVA怎么将一个图片复制到文件夹中去?

JDK宝典里有这样的一段代码,你调用copyFile方法就可以了:

/**

* 复制单个文件, 如果目标文件存在,则不覆盖。

* @param srcFileName 待复制的文件名

* @param destFileName 目标文件名

* @return 如果复制成功,则返回true,否则返回false

*/

public static boolean copyFile(String srcFileName, String destFileName){

return CopyFileUtil.copyFile(srcFileName, destFileName, false);

}

/**

* 复制单个文件

* @param srcFileName 待复制的文件名

* @param destFileName 目标文件名

* @param overlay 如果目标文件存在,是否覆盖

* @return 如果复制成功,则返回true,否则返回false

*/

public static boolean copyFile(String srcFileName,

String destFileName, boolean overlay) {

//判断原文件是否存在

File srcFile = new File(srcFileName);

if (!srcFile.exists()){

System.out.println("复制文件失败:原文件" + srcFileName + "不存在!");

return false;

} else if (!srcFile.isFile()){

System.out.println("复制文件失败:" + srcFileName + "不是一个文件!");

return false;

}

//判断目标文件是否存在

File destFile = new File(destFileName);

if (destFile.exists()){

//如果目标文件存在,而且复制时允许覆盖。

if (overlay){

//删除已存在的目标文件,无论目标文件是目录还是单个文件

System.out.println("目标文件已存在,准备删除它!");

if(!DeleteFileUtil.delete(destFileName)){

System.out.println("复制文件失败:删除目标文件" + destFileName + "失败!");

return false;

}

} else {

System.out.println("复制文件失败:目标文件" + destFileName + "已存在!");

return false;

}

} else {

if (!destFile.getParentFile().exists()){

//如果目标文件所在的目录不存在,则创建目录

System.out.println("目标文件所在的目录不存在,准备创建它!");

if(!destFile.getParentFile().mkdirs()){

System.out.println("复制文件失败:创建目标文件所在的目录失败!" );

return false;

}

}

}

//准备复制文件

int byteread = 0;//读取的位数

InputStream in = null;

OutputStream out = null;

try {

//打开原文件

in = new FileInputStream(srcFile);

//打开连接到目标文件的输出流

out = new FileOutputStream(destFile);

byte[] buffer = new byte[1024];

//一次读取1024个字节,当byteread为-1时表示文件已经读完

while ((byteread = in.read(buffer)) != -1) {

//将读取的字节写入输出流

out.write(buffer, 0, byteread);

}

System.out.println("复制单个文件" + srcFileName + "至" + destFileName + "成功!");

return true;

} catch (Exception e) {

System.out.println("复制文件失败:" + e.getMessage());

return false;

} finally {

//关闭输入输出流,注意先关闭输出流,再关闭输入流

if (out != null){

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (in != null){

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}


网站题目:java代码实现图片拷贝 java图片上传代码
分享地址:http://bjjierui.cn/article/ddjhscd.html

其他资讯