「java图片复制」Java导入图片
本篇文章给大家谈谈java图片复制,以及Java导入图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java中如何将上传的图片复制到指定文件夹中。
- 2、Java如何对压缩包里文件进行操作,把里面的图片文件复制到指定目录(压缩包里面存在文件夹,下面的文件)
- 3、在Java中,怎样将图片从一个地方复制到另一个地方?(最好有代码)
- 4、java 编程,复制图片到另一文件夹下,如何提高效率
- 5、java如何复制文件(包括图片等其他格式的文件)
java中如何将上传的图片复制到指定文件夹中。
public static void copyFile(File sourceFile, File targetFile) throws IOException {
BufferedInputStream inBuff=null;
BufferedOutputStream outBuff=null;
try {
// 新建文件输入流并对它进行缓冲
inBuff=new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff=new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b=new byte[1024 * 5];
int len;
while((len=inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if(inBuff != null)
inBuff.close();
if(outBuff != null)
outBuff.close();
}
}
Java如何对压缩包里文件进行操作,把里面的图片文件复制到指定目录(压缩包里面存在文件夹,下面的文件)
jar命令不是用来执行jar包的,是用来打包的
执行用java -jar
就楼主给的参数,写一个例子好了
1.建立MANIFEST.MF (在D:\workspace\test\class目录下)
以下内容为文件内容
==================
Manifest-Version: 1.0
Main-Class: a(主类的路径,此选项可选,如果希望在windows下双击就执行,必须写)
Created-By: Abc Company(随便写,这个是创建人名字)
==================
2.打包
按照lz给的条件
在D:\workspace\test\classes\目录下执行
jar cvfm classes.jar MANIFEST.MF *.*
以上命令将class下所有文件打包到classes.jar,生成在class目录下
3.执行
输入java -jar classes.jar即可执行
4.另一种方案
如果希望使用非 JAR 工具建立 JAR 文件, 建议使用 WinRAR. 首先还是需要建立一个清单文件(名字必须为MANIFEST.MF), 向此文件中写入同使用 JAR 工具时一样的内容, 接着需要建立一个META-INF子目录, 然后将 MANIFEST.MF 放入此目录, 接着就可以保持目录结构将所有这些内容使用 WinRAR 压缩为 ZIP 格式的文件, 文件名取为 xxx.jar 即可.
在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 编程,复制图片到另一文件夹下,如何提高效率
直接用文件流打开一个文件,在通过楼下说的缓冲流将文件直接写到另外一个文件就可以了
//处理JPEG的
public static String getFixedBoundIcon(String filePath) throws Exception {
//返回地址
String result = "";
//输出流
FileOutputStream out = null;
try {
File f = new File(filePath);
if (!f.isFile()) {
throw new Exception(f + " 不是图片文件!");
}
//图象文件
if (f != null f.exists()) {
//这里的ImageIO属于java工厂类,在工厂类class里面,调用的System.gc(),频繁调用会造成dump,需要考虑优化
BufferedImage image = ImageIO.read(f); // 读入文件
if (image != null) {
BufferedImage tag =
new BufferedImage(116, 165, BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图
tag.getGraphics().drawImage(image, 0, 0, 116, 165, null);
//文件地址部分
int lastLength = filePath.lastIndexOf(".");
String subFilePath = filePath.substring(0, lastLength);
String fileType = filePath.substring(lastLength);
//背景
result = subFilePath + "_116_165" + fileType;
out = new FileOutputStream(result);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param =
encoder.getDefaultJPEGEncodeParam(tag);
param.setQuality(0.95f, true); //95%图像
//像素尺寸单位.像素/英寸
param.setDensityUnit(1);
//水平分辨率
param.setXDensity(300);
//垂直分辨率
param.setYDensity(300);
encoder.setJPEGEncodeParam(param);
encoder.encode(tag);
tag = null;
}
}
f = null;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
out.close();
out = null;
}
return result;
}
还要try起来捕获异常哟
java如何复制文件(包括图片等其他格式的文件)
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
copy("D:\\test.jpg", "D:\\ttt.jpg");
}
public static void copy(String src, String dest) throws IOException {
byte[] buffer = new byte[1024];
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(dest);
int c;
while((c = in.read(buffer)) = 0) {
out.write(buffer, 0, c);
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception err) {
// Ignore the exception.
}
}
if (out != null) {
try {
out.close();
} catch (Exception err) {
//Ignore the exception.
}
}
}
}
}
java图片复制的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java导入图片、java图片复制的信息别忘了在本站进行查找喔。