访问我们的Web应用程序中的外部文件 [英] Accessing External Files Into Our Web Application

查看:142
本文介绍了访问我们的Web应用程序中的外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能会多次被问到,但我找不到直接答案。

This question might be asked many times, but I did not find a direct answer.

实际上我正在使用WebSphere来运行我的Java Web应用程序,我想将我上传的文件/资源​​保存/存储到服务器目录外的文件夹,比如说 D:/ resources / 但是我担心的是,我们可能会这样做吗?因为这可能是一种安全漏洞。

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.

如果有可能,那么如何在Java Web应用程序中执行此操作。

If it is possible then how to do it in Java web applications.

推荐答案

以下是在struts 1.x上传文件的代码

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

这篇关于访问我们的Web应用程序中的外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆