This question might be asked many times, but I did not find a direct answer.
Actually I am using WebSphere for running my Java web application, I wanted to save/store my uploaded files/resources to folder outside server directory, say D:/resources/
but my concern is, is it possible that we do this? As this might be sort of security breach.
If it is possible then how to do it in Java web applications.
Below is code for uploading files in struts 1.x
public class MyModel extends ActionForm {
private static final long serialVersionUID = 1L;
private FormFile file = null;
public void setFile(FormFile file) {
this.file = file;
}
public FormFile getFile() {
return this.file;
}
}
public class FileUploadAction extends DispatchAction{
MyModel m = (MyModel) form;
FormFile myFile = m.getFile();
String contentType = myFile.getContentType();
String fileName = myFile.getFileName();
int fileSize = myFile.getFileSize();
byte[] fileData = myFile.getFileData();
String recieveMaterialFileName = myFile.getFileName();
File newFile = new File("D:/resources/images", recieveMaterialFileName);
if(!newFile.exists()){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(newFile);
fos.write(myFile.getFileData());
fos.flush();
} catch (FileNotFoundException e) {
System.err.println("Error " + e);
} catch (IOException e) {
System.err.println("Error " + e);
}finally{
fos.close();
}
}
}
来源:https://stackoverflow.com/questions/19089175/accessing-external-files-into-our-web-application