Hi,
This is may not a question regarding to the development. But I hope experts
in this mail list can help me. Thanks.
I am experiencing a strange problem when we use FormFile to upload a binary
file.
After the upload, the checksum of binary file is changed, even though the
size is still same (that means the file is changed). But if the file is
text file. Every thing seems fine.
The code I used to do the upload is very similar to the sample code
provided in the struts package. Are there any people in this mail list have
the same problem. Are there any workaround.
Please reply email directly to me or at least CC me, cause I am not in this
mail list. Thanks.
Yiru Zhang
private String uploadFile(FormFile file) throws
FileNotFoundException, IOException{
try {
String uploadpath =
PropService.getProperty("FileManager.TEMP_STORAGE_DIRECTORY");
java.io.File path = new java.io.File(uploadpath);
if (!path.isDirectory() || !path.exists()) {
// System.out.println("upload path" + uploadpath + "is
not there. create it");
path.mkdir();
}
String fileName = file.getFileName();
String contentType = file.getContentType();
String size = file.getFileSize() + "bytes";
InputStream instream = file.getInputStream();
String copyFile = uploadpath + "/"
+fileName;
System.out.println("Copy File:" + copyFile );
OutputStream os = new FileOutputStream(copyFile);
if (file.getFileSize() < (32*1048576)) {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead =
instream.read(buffer,0,8192)) != -1) {
os.write(buffer,0,bytesRead);
}
os.close();
}
else {
System.out.println("the file is greater
than 32MB!");
}
instream.close();
file.destroy();
return copyFile;
} catch (FileNotFoundException fne) {
System.out.println(fne);
throw fne;
} catch (IOException ioe) {
System.out.println(ioe);
throw ioe;
}
}