20180704 struts笔记-文件的上传下载

1.文件上传
    《1》定义页面
         <form action="" method="" enctype="multipart/form-data">
                //文件域
         </form>

  《2》定义action
         action必须定义三个属性
           private File 控件名;
           private String 控件名ContentType;
           private String 控件名FileName;
          //生成get/set方法

         处理方法:
            //1.获得输入流
            FileInputStream in=new FileInputStream(控件名);
            //2.输出流
            String url=
               ServletActionContext.getServletContext().getRealPath("/文件夹名");
            FileOutputStream out=new FileOutputStream(url+"/"+控件名FileName);
            //3.读取写入流
            byte[] b=new byte[1024];
            int num= in.read(b); 
            while(num>0){
               out.write(b,0,b.length);
               num=in.read(b);
             }
           //4.关流
             out.flush();
             out.close();
             in.close();


      《3》在页面中显示
        <image src="<%=basePath%>文件夹名/${控件名FileName}"/>
        示例:
        <image src="<%=basePath%>image/${uploadFileName}"/>  



猜你喜欢

转载自blog.csdn.net/chargingtime/article/details/80909160