zoukankan      html  css  js  c++  java
  • 第二阶段冲刺四

    今天主要完成的任务:学习上传文件原理和解析改文件

    遇到的困难:视频讲解的挺好的,无问题

    解决办法:暂无

    源程序代码实例

    private boolean testFile(HttpServletRequest request)
        {
            boolean flag=false;
            Note note=new Note();
            Map<String, String> fileMap=new HashMap<String, String>();
            String title=request.getParameter("title");
            String see=request.getParameter("see");
            String kemu=request.getParameter("kemu");
            String userID=request.getParameter("userID");
            int userId=Integer.valueOf(userID);
            System.out.println("标题:"+title);
            System.out.println("是否公开:"+see);
            System.out.println("科目:"+kemu);
            System.out.println("用户ID:"+userId);
            
            note.setTitle(title);
            note.setCourse(kemu);
            note.setPermission(see);
            
            DiskFileItemFactory factory=new DiskFileItemFactory();
            ServletFileUpload sfu=new ServletFileUpload(factory);
            sfu.setFileSizeMax(30*1024*1024);
            List<FileItem> list=null;
            String path="/usr/local/CloudNote/";
            String path1="";
            Date t = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
            String date=df.format(t);
            String date_sql=df2.format(t);
            note.setDate(date_sql);
            try {
                list = sfu.parseRequest(request);
            } catch (FileUploadException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return false;
            }
            for(FileItem fileItem:list)
            {
                if(fileItem.isFormField())
                {
            
                    String filename=fileItem.getFieldName();
                    String value=null;
                    try {
                        value = fileItem.getString("utf-8");
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        return false;
                    }
                    if(filename.equals("username")) 
                    {
                        path=path+value;
                        path1=path1+value;
                        File file=new File(path);
                        if(!file.exists()) 
                        {
                            file.mkdir();
                        }
                        path=path+"/"+date;
                        path1=path1+"/"+date;
                        File file2=new File(path);
                        if(!file2.exists()) 
                        {
                            file2.mkdir();
                        }
                    }
                    
                    System.out.println(filename+"="+value);
                }
                else 
                {
                    long size = fileItem.getSize();
                    //判断size是否为0
                    if(size==0){
                        continue;
                    }
                    //获取文件的类型
                    String contentType = fileItem.getContentType();
                    //获取文件的名字
                    String name = fileItem.getName();
                    String key=name;
                    
                    String prefix = UUID.randomUUID().toString().replace("-", "");
                    name = prefix+"_"+name;
                    if(contentType.equals("text/plain"))
                    {
                        fileMap.put("txt_path",path1+"/"+name);
                    }
                    else 
                    {
                        int a=1;
                        if(key.equals("image1.png"))
                        {
                            a=1;
                        }
                        else if(key.equals("image2.png"))
                        {
                            a=2;
                        }
                        else
                        {
                            a=3;
                        }
                        fileMap.put("image"+a+"_path",path1+"/"+name);
                    }
                    //获取表单项的属性名
                    String fieldName = fileItem.getFieldName();
                    System.out.println("文件的大小: "+size);
                    System.out.println("文件的类型: "+contentType);
                    System.out.println("文件的名字: "+name);
                    System.out.println("表单项name属性名: "+fieldName);
                    //将文件写入到磁盘中
                    try {
                        fileItem.write(new File(path+"/"+name));
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        
                    }
                }
            }
            note.setFileMap(fileMap);
            try {
                userService=UserService.getUserService();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag=false;
            }
            flag=userService.addNote(note,userId);
            return flag;
        }    
  • 相关阅读:
    制作USB系统盘
    01Mysql 配置方法
    Tec_010_怎样看K线图
    回顾5年内的央行加息历史
    推荐:微软下一代操作系统Windows 7版本详解
    关于USB启动盘制作
    Delphi Program test
    圣诞节 玩具
    敏捷宣言
    [转 TDD] 如何坚持TDD:使用者出现的问题以及解决方案
  • 原文地址:https://www.cnblogs.com/weixiao1717/p/13085176.html
Copyright © 2011-2022 走看看