0

Fileupload wrong file size

asked 2012-08-23 11:35:33 +0800

flflfl gravatar image flflfl
51

Hey,

I've a problem with the file upload, the uploaded files have the wrong size.

For example:
Original File: 1.527.148 Bytes
File on Server: 1.523.712 Bytes

Here is my code:

UploadEvent event = (UploadEvent) arg0;
media = event.getMedia();

if(media.getByteData().length > 10 * 1024 * 1024) {
	Messagebox.show(err_msg, err_head, Messagebox.OK,
			Messagebox.ERROR);
}else{
	String[] data_type = media.getContentType()
			.split("/");
	String file_type = ".apk";
	if(data_type.length > 1) {
		file_type = data_type[1];
	}
	
	File f = new File("/home/flo/Desktop/"+System.currentTimeMillis()+"."+file_type);
	if(f.exists()) {
		f.delete();
	}
	
	BufferedInputStream in = null;
	BufferedOutputStream out = null;
	
	InputStream fin = media.getStreamData();			
	in = new BufferedInputStream(fin);
	
	OutputStream fout = new FileOutputStream(f);
	out = new BufferedOutputStream(fout);
	byte buffer[] = new byte[1024];
	int ch = in.read(buffer);
	while (ch != -1) {
		out.write(buffer, 0, ch);
		ch = in.read(buffer);
	}	
}

zul:

<button label="Choose file..." width="120px" id="attachBtn"
					sclass="attachBtn" upload="true" />


Whats wrong here?

Thanks,
flo

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2012-08-23 15:31:43 +0800

jj gravatar image jj
638 3

If it is a text file, and server/client are different OS, they may be of different size. Did you check if the uploaded file is correct?

link publish delete flag offensive edit

answered 2012-08-23 15:52:57 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

You are not closing the streams, so the data is not flushed and in addition you are leaking a file handle.

If you write IO code yourself, always close streams in a finally block!

try {
  BufferedInputStream in = null;
  BufferedOutputStream out = null;
	
  InputStream fin = media.getStreamData();			
  in = new BufferedInputStream(fin);
	
  OutputStream fout = new FileOutputStream(f);
  out = new BufferedOutputStream(fout);
  byte buffer[] = new byte[1024];
  int ch = in.read(buffer);
  while (ch != -1) {
    out.write(buffer, 0, ch);
    ch = in.read(buffer);
  }
} finally {
  if (out != null) {
    try {
      out.close();
    } catch (IOException e) {
    }
  }
  if (in != null) {
    try {
      in.close();
    } catch (IOException e) {
    }
  }
}

OR add Commons IO library to your project and use this much better code:

InputStream in = null;
OutputStream out = null;
try {
  in = media.getStreamData();
  out = new FileOutputStream(f);
  IOUtils.copy(in, out);
} finally {
  IOUtils.closeQuietly(in);
  IOUtils.closeQuietly(out);
}

link publish delete flag offensive edit

answered 2012-09-12 14:25:33 +0800

flflfl gravatar image flflfl
51

Hi gekkio,

you're right, I forgot to close the streams. I'm using IOUtils now!

Thank you!

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-08-23 11:35:33 +0800

Seen: 133 times

Last updated: Sep 12 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More