2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Accessing External Files Into Our Web Application.

I am working onWebSphere 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.
userimage

You its possible with below snippet by using Struts 1.x, Hope it will helpful


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();
        }
    }
}

Answer is