Create a copy document from document and media into portlet local temp folder

OutputStream outputStream = null;
InputStream inputStream = null;
try {
DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(targetGroupId, folderId , filename);
inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(themeDisplay.getUserId(),fileEntry .getFileEntryId(), fileEntry .getVersion());
String tempFolderPath = resourceRequest.getPortletSession().getPortletContext().getRealPath(File.separator) + "temp"+File.separator;
 
File tempFolder = new File(tempFolderPath);
if (!tempFolder.exists()) {
   try{
    tempFolder.mkdir();
   }catch(Exception se){
       se.printStackTrace();
   } 
}else{
String[] files;
       if (tempFolder.isDirectory()) {
        files = tempFolder.list();
           for (int i = 0; i < files.length; i++) {
               File file = new File(tempFolder, files[i]);
               if (!file.isDirectory()) {
                file.delete();
               }
           }
       }
}
 
File file = new File(tempFolderPath+File.separator+spreadSheetName);
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
 
spreadSheetPath = file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
if(outputStream != null)
outputStream.close();
if(inputStream != null)
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}