我一次上传了多个文件。我在Java端使用Apache Commons文件上传,我需要单个文件的文件大小。但是,我正在获得总内容的大小。 Java代码::
public ModelAndView addDocument(HttpServletRequest request, HttpServletResponse response){
// find the file size..
LOG.debug("Request Content length: "+request.getContentLength());
String mainInd = request.getParameter("main");
String supportInd = request.getParameter("support");
int contentLength = request.getContentLength();
System.out.println("Content Length :: " + contentLength);
if (mainInd != null && "main".equalsIgnoreCase(mainInd))
{
if(FileSizeUtility.isMaxFileSizeExceeded(contentLength)){
ModelAndView mav = new ModelAndView("ajaxresponse/filesUploaded");
mav.addObject("success", "false");
mav.addObject("fileList", new ArrayList());
mav.addObject("totalCount", 0);
mav.addObject("message", AppConfig.getInstance().get("upload.file.exceed"));
return mav;
}
}
if (supportInd != null && "support".equalsIgnoreCase(supportInd))
{
if(FileSizeUtility.isMaxSupFileSizeExceeded(contentLength)){
ModelAndView mav = new ModelAndView("ajaxresponse/filesUploaded");
mav.addObject("success", "false");
mav.addObject("message", AppConfig.getInstance().get("upload.sup.file.exceed"));
return mav;
}
}
System.out.println("Content Length check :: " + FileSizeUtility.isMaxSupFileSizeExceeded(contentLength));
System.out.println("Support Inc :: " + supportInd);
LOG.debug("restricted files names: "+ddxExpnList);
//time to know the upload duration
Date time1 = new Date();
String rptTempDirPath = TEMP_PATH + new Date().getTime();
File rptTempDir = new File(rptTempDirPath);
//temp dir created for upload file.
rptTempDir.mkdirs();
LOG.debug("temp path for upload:"+rptTempDirPath);
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("isMultipart="+isMultipart);
//ERS-62
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(MAX_MEM_SIZE_DOWNLOAD);
if(rptTempDir.exists()){
// Location to save data that is larger than maxMemSize.
factory.setRepository(rptTempDir);
}
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
List<RptValueObject> fileList = new ArrayList<RptValueObject>();
//testUpload(upload , request);
try{
LOG.debug("In try catch");
FileItemIterator iter = upload.getItemIterator(request);
System.out.println("Entering loop.."+iter.hasNext());
while (iter.hasNext()) {
LOG.debug(" reading items...");
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
LOG.debug("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
String fileName = item.getName();
if(StringUtils.hasText(fileName))
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
fileName = validateAndmodifyFileName(fileName);
fileName = fileName.replace('+', '_');
fileName = fileName.replace(' ', '_');
// find file type and make it uppercase
String fileType = fileName.substring(fileName.lastIndexOf('.'));
fileName = fileName.substring(0, fileName.lastIndexOf('.'))+fileType.toLowerCase();
System.out.println("New name is "+fileName);
System.out.println(item.getContentType().length());
if(!ValidationCommonUtility.validateFileType(fileName))
throw new BaseException(AppConfig.getInstance().get("file.invalid.format"));
// Process the input stream
if(tempUploadSession.getSessionId()==null)
tempUploadSession.setSessionId(request.getSession().getId());
//ERS-62
long fileSize = tempUploadSession.uploadFile(item.openStream(), fileName, contentLength);
double sizeInKB = FileSizeUtility.convertByteToKB(fileSize);
System.out.println("File size is "+fileSize);
System.out.println("File size is KB "+sizeInKB);
if(LOG.isDebugEnabled()){
LOG.debug("File Size is :::: "+sizeInKB);
LOG.debug("Display Value is :::: "+FileSizeUtility.getFileSizeForDisplay(sizeInKB));
}
fileList.add(new RptValueObject(fileName, FileSizeUtility.getFileSizeForDisplay(sizeInKB),"","",""));
}
}
}catch(IOException ioe){
}catch(FileUploadException fue){
} catch (Exception e) {
}
ModelAndView mav = new ModelAndView("ajaxresponse/filesUploaded");
mav.addObject("success", "true");
mav.addObject("fileList", fileList);
mav.addObject("totalCount", fileList.size());
//remove the temp directory created for file upload
if(rptTempDir.exists()){
rptTempDir.delete();
}
//time to know the upload duration
Date time2 = new Date();
long duration = time2.getTime()-time1.getTime();
System.out.println("Total time took for upload:"+duration+"milliseconds"+" OR "+duration/1000+"seconds");
return mav;
}
我无法获取请求中单个文件的大小。如何获取每个文件的大小。
任何帮助将不胜感激。