This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12948|Reply: 0

[JavaEE] struts2file upload

[Copy link]
Posted on 12/24/2014 2:12:11 AM | | |
[mw_shl_code=java,true]
  <body><%--
  The ORM form is queried in binary format, and you need to set enctype="multipart/form-data"
    --%><form action="${pageContext.request.contextPath}/de/ServletActionupload"  enctype="multipart/form-data" method="post"/>
            file<input type="file" name="image"/>
            <input type="submit" value="upload"/>
    </form>
  </body>

        <action name="ServletAction*" class="action.ServletAction" method="{1}">
                <result name="message">/index.jsp</result>
                <result name="uploadmsg">/uploadmsg.jsp</result>
                </action>

...
private File image;
        private String imageFileName; The field name is followed by the FileName
       
       
       
        public String getImageFileName() {
                return imageFileName;
        }

        public void setImageFileName(String imageFileName) {
                this.imageFileName = imageFileName;
        }

        public File getImage() {
                return image;
        }

        public void setImage(File image) {
                this.image = image;
        }


/**
         * Upload files
         * @return
         * @throws IOException
         */
        public String upload(){
               
                        try {
                                String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image!=null) {
                                File savefile = new File(new File(realpath),imageFileName);
                                if (!savefile.getParentFile().exists()) {
                                        savefile.getParentFile().mkdir();
                                }
                                FileUtils.copyFile(image,savefile);
                                ActionContext.getContext().put("msg", "Upload successful!");
                               
}else {
ActionContext.getContext().put("msg", "Please select file");
}
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                ActionContext.getContext().put("msg", "Upload failed!");
                        }
                return "uploadmsg";
        }


[/mw_shl_code]




Previous:I feel that this forum is so deserted, just post a picture, no one is reading it anyway. Ha
Next:struts2 custom time converter
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com