07-06
24

<html:file>上传文件标签

       利用Struts提供的<html:file>来做上传图片。
Jsp页:
<html:errors/>
<html:form action="upload.do" method="POST" enctype="multipart/form-data">
<html:file property="file"/><br>
<html:submit property="submit" value="Submit"/><br>
</html:form>


form.java
package upload;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;

public class FileForm extends ActionForm {
    private FormFile file;
    public FormFile getFile() {
        return file;
    }
    public void setFile(FormFile file) {
        this.file = file;
    }
   public ActionErrors validate(ActionMapping actionMapping,
                                 HttpServletRequest httpServletRequest) {
        /** 先验证是否有文件,再验证文件格式.*/
        ActionErrors errors = new ActionErrors();
        if (file == null || file.equals("")) {
            errors.add("ext", new ActionMessage("ext"));
            return errors;
        }
        String fullname = file.getFileName();
        String ext = fullname.substring(fullname.indexOf("."), fullname.length());
        if (!(ext.equalsIgnoreCase(".jpg") || ext.equalsIgnoreCase(".gif") ||
              ext.equalsIgnoreCase(".png"))) {
            System.out.println("封装错误信息");
            errors.add("ext", new ActionMessage("ext"));
        }
        return errors;
    }

    public void reset(ActionMapping actionMapping,
                      HttpServletRequest servletRequest) {
    }
}


Action.java
package upload;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;

public class UploadAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws
            FileNotFoundException, IOException {

        String dir = servlet.getServletContext().getRealPath(""); //根目录
        FileForm fileForm = (FileForm) form;
        FormFile file = fileForm.getFile();
        String fname = file.getFileName();
        InputStream streamIn = file.getInputStream(); //输入流
        OutputStream streamOut = new FileOutputStream(dir + "/" + fname);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
            streamOut.write(buffer, 0, bytesRead);
        }
        streamIn.close();
        streamOut.close();
        return new ActionForward("/success.jsp");
    }
}


      可能是我基础太差了,写validator验证写了半天...另外,资源文件和配置信息略

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 982
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭