Java Uploaded file is not the same -


my problem following, i'm trying upload file server via servlet uploaded file not same, has differences compared original one.

for example, if upload txt file example

# root logger option log4j.rootlogger=debug, stdout, file  # redirect log messages console log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.target=system.out log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss} %-5p     %c{1}:%l - %m%n  # rirect log messages log file log4j.appender.file=org.apache.log4j.rollingfileappender log4j.appender.file.file=c:\\fileslog\\log.log log4j.appender.file.maxfilesize=5mb log4j.appender.file.maxbackupindex=10 log4j.appender.file.layout=org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss} %-5p         %c{1}:%l - %m%n 

the upload file has new line with

... nulnulnulnulnulnul

(opened notepad++).

if file .exe, uploaded file doesn't work.

my code following

protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     try {          boolean ismultipart = servletfileupload.ismultipartcontent(request);         system.out.println("es un request multipart " + ismultipart);         // set factory constraints         // create new file upload handler         servletfileupload upload = new servletfileupload();         // set overall request size constraint         // max request size         // parse request         // list / fileitem / items = upload.parserequest(request);         fileitemiterator iter = upload.getitemiterator(request);         while (iter.hasnext()) {             // fileitem item = (fileitem) iter.next();             fileitemstream items = (fileitemstream) iter.next();              if (items.isformfield() == false) {                 // process file upload                 string filename = items.getname();                 system.out.println("file name " + filename);                 // process file upload                 file uploadedfile = new file("c:\\filesupload\\" + uuid.randomuuid());                 // inputstream uploadedstream = item.getinputstream();                  fileoutputstream fout = new fileoutputstream(uploadedfile);                 bufferedoutputstream bout = new bufferedoutputstream(fout);                 bufferedinputstream bin = new bufferedinputstream(items.openstream());                 byte buf[] = new byte[2048];                 long contador = 0l;                 while ((bin.read(buf)) != -1) {                     bout.write(buf);                     contador = contador + 1l;                 }                 bout.close();                 bin.close();             }         }     } catch (exception e) {         e.printstacktrace();     } } 

in part of code:

 while ((bin.read(buf)) != -1) {         bout.write(buf);         contador = contador + 1l;     } 

you not checking how many bytes have been read. last buffer read, if length of file not exactly divisible 2048, read partially. in case, bin.read(buf) return number of bytes read, , should use number when write buf, otherwise you'll write 2048 bytes in buf, including values previous reads or zeros if first read.

so should this:

 int numbytesread;  while ((numbytesread = bin.read(buf)) != -1) {         bout.write(buf,0,numbytesread);         contador = contador + 1l;  } 

this make write read , no more.

by way, if using java 7 , above, instead of doing this, can use files.copy copy whole stream directly file.

files.copy(items.openstream(),            paths.get("c:\\filesupload\\" + uuid.randomuuid()),            standardcopyoption.replace_existing ); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -