首页 > 其他 > 详细

struts2 File标签上传文件

时间:2014-07-24 17:35:05      阅读:275      评论:0      收藏:0      [点我收藏+]

1、action.class

private File pictureType;
    private String pictureTypeContentType;
    private String pictureTypeFileName;

public String editType() throws IOException{
        Type t=typeService.find(Type.class, id);
        System.out.println("id="+id);
        System.out.println(UploadPath);
        System.out.println(pictureTypeFileName);

        File saved=new File(UploadPath+Constants.TypeDir, t.getId()+"."+pictureTypeContentType.split("/")[1]);

        InputStream ins=null;
        OutputStream ous=null;
        try {
            saved.getParentFile().mkdirs();
            ins=new FileInputStream(pictureType);
            ous=new FileOutputStream(saved);
            byte[] b=new byte[1024];
            int len=0;
            while((len=ins.read(b))!=-1){
                ous.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(ous!=null){
                ous.close();
            }
            if(ins!=null){
                ins.close();
            }
            t.setPicUrl(Constants.TypeDir+"/"+t.getId()+"."
                    + pictureTypeContentType.split("/")[1]);
            typeService.save(t);
        }
        return SUCCESS;
    }

 

2、表单

<s:form action="editType_manage" enctype="multipart/form-data">
        <s:file label="修改商店图片" name="pictureType"></s:file>
        <s:submit value="确认修改" />
    </s:form>

3、action得不到文件的几个原因

(1)三个变量,没有get-set方法

(2)Form没有设置enctype="multipart/form-data"

struts2 File标签上传文件,布布扣,bubuko.com

struts2 File标签上传文件

原文:http://www.cnblogs.com/xingyyy/p/3865656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!