zoukankan      html  css  js  c++  java
  • java上传文件

    jsp页面

    form中的enctype属性应设置为:multipart/form-data

    <form action="/uplode.do" method="post" enctype="multipart/form-data">
    <td>   <input type="file" id="file" name="file" value="file" onchange="fz();"/>   <input type="hidden" id="filepath" name="attachment.filepath" value="$attachment.filepath"/>   <input type="hidden" id="filename" name="attachment.filename" value="$attachment.filename"/>   <input type="hidden" id="suffix" name="attachment.suffix" value="$attachment.suffix"/> </td>
    </form>
      function fz(){
           var aa=document.getElementById("file").value;
           var arrS2=aa.split("\");
           var i=arrS2.length;
           var filename = arrS2[i-1]
           var arrb = aa.split(".");
           var j = arrb.length;
           document.getElementById("file").value=aa;
           document.getElementById("filename").value=filename;
           document.getElementById("filepath").value="/uploadfiles/casus/"+filename;   
           document.getElementById("suffix").value=arrb[j-1];
        }

    action:

         //附件相关
        private AttachmentMatters  attachment;
        private String   fileName; //名字
        private String suffix; //后缀
        private String  file; 
        private String attachmentid;
       /**
        * 上传   */
        public void uploadFile(){ 
            try{
                String name = attachment.getFilename();
                upload(this.file,name) ; //上传到服务器     

           //保存到附件表里      
                if(name!=null&&!name.equals("")){
                    attachment.setFilepath("uploadfiles/sss/"+name);   
                    attachment.setUploadDate(new Date()); 
                    bean.saveObject(attachment);                
                }                                        
               }catch(Exception e){
                e.printStackTrace();
             }  
        } 
    public static void upload(String file, String name){ 
            if(file !=null){             
                FileOutputStream outputStream; 
                try { 
                    String path=ServletActionContext.getRequest().getRealPath("/uploadfiles/matters");
                    String path1=path;
                    path1.replaceAll("\u002E\u002E", "2");                
                    String fileDir = path+File.separator; 
                    String filePath=fileDir+name; 
                    
                    File f=new File(fileDir); 
                    f.mkdirs();                 
                    outputStream = new FileOutputStream(filePath); 
                    FileInputStream fileIn = new FileInputStream(file); 
                    byte[] buffer = new byte[128]; 
                    int len; 
                    while ((len = fileIn.read(buffer))>0){ 
                        outputStream.write(buffer, 0, len); 
                    } 
                    fileIn.close(); 
                    outputStream.close(); 
                }catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            }else{ 
              System.out.println("文件为空"); 
            } 
        }        
  • 相关阅读:
    C#编程思路
    将字符串类型字段转为map类型字段,使用str_to_map()函数
    写hive脚本时,如果hive的过滤条件比较多。可以把过滤条件放到一个参数里。然后把参数放到过滤条件处。这样以后只需要改参数就可以了
    linux中. 路径/文件
    inner join ,left join 会导致数据发散
    如何批量按分区插入数据
    hive表添加字段后,查不出数据是咋回事?
    linux中$0的含义
    linux中的$#含义
    linux的语法
  • 原文地址:https://www.cnblogs.com/estellez/p/3941829.html
Copyright © 2011-2022 走看看