公共无效文件上传(字符串文件名)抛出异常{
// file
File file = new File(filename);
if (file.exists()) {
long folderId = 12344;
long repositoryId = 10180;
// filename
String sourceFileName = file.getName();
// mimetype
String mimeType = "";
if (sourceFileName.substring(sourceFileName.length() - 4)
.compareToIgnoreCase(".pdf") == 0) {
mimeType = "application/pdf";
} else {
if (sourceFileName.substring(sourceFileName.length() - 4)
.compareToIgnoreCase(".xls") == 0) {
mimeType = "application/xls";
}
}
// for title
String title = file.getName();
// for description
String description = file.getName();
// changeLog
String changeLog = "";
// fileEntryTypeId
long fileEntryTypeId = 0;
// for fieldMap
Fields feild = null;
Map<java.lang.String, com.liferay.portlet.dynamicdatamapping.storage.Fields> fieldsMap = new HashMap<java.lang.String, com.liferay.portlet.dynamicdatamapping.storage.Fields>();
fieldsMap.put("", feild);
// for InputStream
InputStream is = new FileInputStream(file);
// for file size
double bytes = file.length();
long size = (long) (bytes / 1024);
// service Context Object
String uuid = UUID.randomUUID().toString();
Date date = new Date();
ServiceContext serviceContext = new ServiceContext();
serviceContext.setUuid(uuid);
serviceContext.setCreateDate(date);
serviceContext.setModifiedDate(date);
// PermissionChecking
PrincipalThreadLocal.setName(10196);
PermissionChecker permissionChecker = PermissionCheckerFactoryUtil
.create(UserLocalServiceUtil.getUser(10196));
PermissionThreadLocal.setPermissionChecker(permissionChecker);
DLAppServiceUtil.addFileEntry(repositoryId, folderId,
sourceFileName, mimeType, title, description, changeLog,
is, size, serviceContext);
/*
* DLFileEntryLocalServiceUtil.addFileEntry(userId, groupId,
* repositoryId, folderId, sourceFileName, mimeType, title,
* description, changeLog, fileEntryTypeId, fieldsMap, file, is,
* size, serviceContext);
*/
}
}