Accessing External Files Into Our Web Application

社会主义新天地 提交于 2019-12-02 15:51:18

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!