Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static List<FileMeta> uploadByJavaServletAPI(HttpServletRequest request)
// 3.2 Create a new FileMeta object
temp = new FileMeta();
temp.setFileName(getFilename(part));
temp.setFileSize(part.getSize()/1024 +" Kb");
temp.setFileSize(humanReadableByteCount(part.getSize(), true));
temp.setFileType(part.getContentType());
temp.setContent(part.getInputStream());
temp.setTwitter(twitter);
Expand Down Expand Up @@ -91,7 +91,7 @@ public static List<FileMeta> uploadByApacheFileUpload(HttpServletRequest request
temp.setFileName(item.getName());
temp.setContent(item.getInputStream());
temp.setFileType(item.getContentType());
temp.setFileSize(item.getSize()/1024+ "Kb");
temp.setFileSize(humanReadableByteCount(item.getSize(), true));

// 2.7 Add created FileMeta object to List<FileMeta> files
files.add(temp);
Expand All @@ -110,7 +110,16 @@ public static List<FileMeta> uploadByApacheFileUpload(HttpServletRequest request
}
return files;
}


// convert byte size into human readable format
//
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

// this method is used to get file name out of request headers
//
Expand Down