• struts2 File标签上传文件


    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"

  • 相关阅读:
    epoll oneshot
    回望五月
    都知道的copy_from_user
    ixgbe 驱动 为xxx驱动做准备1
    面试问题集锦
    数据治理
    阅读
    hive 数据仓库面试题目集锦
    面试小问题集锦
    Scala学习笔记~尚硅谷学习视频
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3865656.html
Copyright © 2020-2023  润新知