Accessing External Files Into Our Web Application

后端 未结 1 863
暗喜
暗喜 2021-01-26 09:53

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 m

相关标签:
1条回答
  • 2021-01-26 10:46

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题