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("文件为空"); 
            } 
        }        
  • 相关阅读:
    Java锁到底锁的到底是哪个对象?什么是锁对象
    什么是正向代理,什么是反向代理
    到底什么是线程安全
    为什么要使用接口,直接写是实现类不行吗
    Nginx配置学习(一)
    Zookeeper集群节点数量为什么要是奇数个?
    Redis 5 单实例数据迁移到Cluster
    Centos8安装Nginx1.18.0
    vmware workstation15 桥接模式互ping不通,虚机可以连通局域网其他机器解决方法
    MongoDB double类型保留2位小数
  • 原文地址:https://www.cnblogs.com/estellez/p/3941829.html
Copyright © 2011-2022 走看看