struts2文件上传

来源:互联网 发布:java仿qq好友列表 编辑:程序博客网 时间:2024/06/05 13:31

单文件上传:
1.先导入commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar
2.前端jsp代码

    <div>        <label for="file">上传头像:</label> <input type="file" id="img" name="img">    </div>

3.后端action的接收

    //注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件    private File img;    //提交过来的file的名字    private String imgFileName;    //提交过来的file的MIME类型    private String imgContentType;public String register() throws Exception{        if (img!=null) {            String imageName=DateUtil.getCurrentDateStr();            String realPath=ServletActionContext.getServletContext().getRealPath("/image/bloger");            String imageFile=imageName+"."+imgFileName.split("\\.")[1];            String []names = img.getName().split("\\.");            File saveFile=new File(realPath,imageFile);            FileUtils.copyFile(img, saveFile);            bloger.setImageName("image/bloger/"+imageFile);  }}

4.在struts.xml中设置struts.multipart.saveDir属性和值。
http://jingyan.baidu.com/album/2a138328aed685074a134fca.html

0 0