在项目中要做图片上传功能,由于用了struts2 文件上传方便了很多,在actoin中增加如下方法就可以了:
private static final int BUFFER_SIZE = 16 * 1024;
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream ut = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
ut = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
文件上传很简单就实现了,可是突然我发现上传的文件都比原来大十几K.由于系统用户量很大,如果有一百万张,那占的空间可非常大了,查看jdk文档,其中在BufferedOutputStream文档中有write(byte[] b, int off, int len)和write(byte[] b).其中write(byte[ ] b,int off,int len)注释为:
将指定 byte 数组中从偏移量off开始的len个字节写入此缓冲的输出流。一般来说,此方法将给定数组的字节存入此流的缓冲区中,根据需要将该缓冲区刷新,并转到底层输出流。但是,如果请求的长度至少与此流的缓冲区大小相同,则此方法将刷新该缓冲区并将各个字节直接写入底层输出流。因此多余的BufferedOutputStream将不必复制数据。
果然我试了write(byte[ ] b,int off,int len)方法文件没有变大.由于习惯,在跟踪下去看看是究竟怎么回事.最后发现write(byte[ ]) 是调用了write(byte[] b,int off,int len),其中len数组的长度.问题就出在这里.在最后一次写入流时,len一般不会为读入字节的长度.除非文件大小刚好被BUFFER_SIZE整除.而通过 while ((len = in.read(buffer)) > 0){write(byte[] b,int off,int len) ,其中len为实际读入流的字节长度.所以这个方法不会增加文件大小,不会把多余的字节写进去.
也不知道为什么sun在java中增加了write(byte[] b) 方法,不过该方法不会影响文件.当我用MagickImage处理一把,文件就恢复原样了.
技术真要求精益求精,多思考,才能多领会.
分享到:
相关推荐
java集合类的文件,//Student s = new Student(); s = null;... void write(byte[] b,int off,int len) 写出某个范围的字节道数组b中 b 数据写出的数组 off 第一个写出字节在b中的偏移量
public synchronized int write(byte[] b,int nStart,int len){ int n=-1; try{ rf.write(b,nStart,len); n=len; }catch(IOException ioe){ ioe.printStackTrace(); } return n; } public ...
java中实现复制文件和文件夹 public static void copyFile(File sourceFile,File targetFile) throws IOException{ // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile)...
今天小编就为大家分享一篇关于Java中byte输出write到文件的实现方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Write Off
Write Java program called AverageNumbers.java to determine the average of several numbers.
output.write(b, 0, len); } output.flush(); output.close(); input.close(); } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } ...
三菱FX系列PLC编程口通讯程序之字节读取(发布源码)
java读取Excel表格,拷贝、更新Excel工作薄
int len; ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); while (true) { len = in.read(buf); if (len == -1) { new TCPClient(bufOut.toByteArray(), InetAddress.getLocalHost(), 1211); ...
//// byte[] b = new byte[1024]; //// while(in.read(b) != -1){ //// out.write(b); //// } // //操作流 法三 // byte[] b = new byte[1024]; // int length = 0; // while((length = in.read(b)...
Design and write a Java class named SalesArray.java that will: 1. Declare a two-dimensional integer array named sales. Populate the first four columns using the following data. Quarter 1 Quarter 2 ...
int len; while ((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e....
import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com.swetake.util.Qrcode; public class ...
PEP人教版小学英语五年级下册Unit 5 B Read and write教学反思.pdf
void write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。 最常用 * //2. void write(byte[] b)将 b.length个字节从指定的字节数组写入此文件输出流。 ...
* Write binary byte array to local file. * * @param fileName - * local file name will write to * @param content * binary byte array * @return true if success * @throws IOException *...
用 Java 编写的、协议和平台都独立的服务器端组件,使用请求/响应的模式,提供了一个基于 Java 的服务器解决方案。本文将从文件传输的基本原理入手,分析如何用 java进行文件的上传,并提出解决方案。 一、基本...
Java实现压缩与解压缩ZIP import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;...
主要为大家详细介绍了Java中FileOutputStream流的write方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下