Sample IO Files Methods
How to delete file:
private void deleteOldFiles() {
String path = JSFUtils.getContextPath();
File userDirectory = new File(path + "\\tempDoc\\" + ADFContext.getCurrent().getSessionScope().get("UserName"));
if (userDirectory.exists()) {
try {
FileUtils.deleteDirectory(userDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
}
how to upload array of files
private void uploadTempFiles() {
String path = JSFUtils.getContextPath();
System.out.println("context path is " + path);
File userDirectory = new File(path + "\\tempDoc\\" + ADFContext.getCurrent().getSessionScope().get("UserName"));
if (!userDirectory.exists()) {
userDirectory.mkdirs();
}
List<String> alreadyProcessedFiles = new ArrayList<String>();
List<UploadedFile> files = this.getUploadedFile();
InputStream is = null;
FileOutputStream out = null;
String sourceFileName = null;
if (files != null) { // if there are files then copy these file to the user direcoty
for (int i = 0; i < files.size(); i++) {
sourceFileName = files.get(i).getFilename();
if (!alreadyProcessedFiles.contains(sourceFileName)) {
try {
is = files.get(i).getInputStream();
out =
new FileOutputStream(path + "\\tempDoc\\" +
ADFContext.getCurrent().getSessionScope().get("UserName") + "\\" +
sourceFileName);
if (is != null && out != null) {
IOUtils.copy(is, out);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
alreadyProcessedFiles.add(sourceFileName);
}
}
}
}
private void deleteOldFiles() {
String path = JSFUtils.getContextPath();
File userDirectory = new File(path + "\\tempDoc\\" + ADFContext.getCurrent().getSessionScope().get("UserName"));
if (userDirectory.exists()) {
try {
FileUtils.deleteDirectory(userDirectory);
} catch (IOException e) {
e.printStackTrace();
}
}
}
how to upload array of files
private void uploadTempFiles() {
String path = JSFUtils.getContextPath();
System.out.println("context path is " + path);
File userDirectory = new File(path + "\\tempDoc\\" + ADFContext.getCurrent().getSessionScope().get("UserName"));
if (!userDirectory.exists()) {
userDirectory.mkdirs();
}
List<String> alreadyProcessedFiles = new ArrayList<String>();
List<UploadedFile> files = this.getUploadedFile();
InputStream is = null;
FileOutputStream out = null;
String sourceFileName = null;
if (files != null) { // if there are files then copy these file to the user direcoty
for (int i = 0; i < files.size(); i++) {
sourceFileName = files.get(i).getFilename();
if (!alreadyProcessedFiles.contains(sourceFileName)) {
try {
is = files.get(i).getInputStream();
out =
new FileOutputStream(path + "\\tempDoc\\" +
ADFContext.getCurrent().getSessionScope().get("UserName") + "\\" +
sourceFileName);
if (is != null && out != null) {
IOUtils.copy(is, out);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
alreadyProcessedFiles.add(sourceFileName);
}
}
}
}
Comments