Accessing External Files Into Our Web Application

做~自己de王妃 提交于 2019-12-02 09:47:31

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