File upload using Guava and apache common? -
i use guava , apache commons convert temporary image have been loaded server conversion result corrupted file. problem "samplefile" corrupted , don't know why until have no error.
import com.google.common.io.files; import org.apache.commons.codec.binary.base64; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.util.uuid; public class imagedecoder { public static void main(string[] args) { byte[] data = null; final file input = new file("c:\\users\\xxx\\appdata\\local\\temp\\multipartbody2180016028702918119astemporaryfile"); try { data = base64.decodebase64(files.tobytearray(input)); } catch (exception ex) { system.out.print("problem"); } final file f = new file(string.format("samplefile_%s.jpg", uuid.randomuuid())); try { if (!f.exists()) f.createnewfile(); final fileoutputstream fos = new fileoutputstream(f); fos.write(data); fos.flush(); fos.close(); } catch (filenotfoundexception e) { system.out.print("file not found"); } catch (ioexception e) { system.out.print("exception"); } } }
based on comment, sounds the original file (input
) contains actual bytes of image. reason, you're reading bytes in if base 64 encoded ascii representation of image. aren't, why doing this? if skip base64.decodebase64
step, i'm guessing things work expected.
of course, in case you're doing copying bytes of input
f
, simpler , more efficient as:
files.copy(input, f);
or if have no need leave temporary file is, moving file better:
files.move(input, f);
Comments
Post a Comment