Home >Java >JavaBase >How to upload files in java

How to upload files in java

(*-*)浩
(*-*)浩Original
2019-11-14 11:58:273527browse

How to upload files in java

There are many ways to upload files in Java, the simplest ones are still FileInputStream and FileOutputStream. Here I list 3 common file upload method codes

First of all, using the springMVC native file upload method requires some simple configuration. Not much to say, as shown above. (Recommended learning: java course)

How to upload files in java1. Use the method of uploading files provided by spring

@RequestMapping("springUpload")
    public String  springUpload(HttpServletRequest request) throws IllegalStateException, IOException
    {
         long  startTime=System.currentTimeMillis();
         //将当前上下文初始化给  CommonsMutipartResolver (多部分解析器)
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
           //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();
            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null)
                {
                    String path="E:/springUpload"+file.getOriginalFilename();
                    //上传
                    file.transferTo(new File(path));
                } 
            }
        }
        long  endTime=System.currentTimeMillis();
        System.out.println("Spring方法的运行时间:"+String.valueOf(endTime-startTime)+"ms");
        return "/success"; 
    }

2. Use file.Transto to save uploaded files. This is currently the best upload method in my opinion, and it is also my favorite upload method. The code is simple and the speed is fast. Please see the code below.

/*
     * 采用file.Transto 来保存上传的文件
     */
    @RequestMapping("fileUpload2")
    public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file) throws IOException {
         long  startTime=System.currentTimeMillis();
        System.out.println("fileName:"+file.getOriginalFilename());
        String path="E:/"+new Date().getTime()+file.getOriginalFilename();
          
        File newFile=new File(path);
        //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
        file.transferTo(newFile);
        long  endTime=System.currentTimeMillis();
        System.out.println("采用file.Transto的运行时间:"+String.valueOf(endTime-startTime)+"ms");
        return "/success"; 
    }

3. The third method is to upload by stream. This method is often used by novices when learning, but I don’t like it because it is slow and difficult to write. Please see

@RequestMapping("fileUpload")
    public String  fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException {
          
        //用来检测程序运行时间
        long  startTime=System.currentTimeMillis();
        System.out.println("fileName:"+file.getOriginalFilename());
          
        try {
            //获取输出流
            OutputStream os=new FileOutputStream("E:/"+new Date().getTime()+file.getOriginalFilename());
            //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
            InputStream is=file.getInputStream();
            byte[] bts = new byte[1024];
            //一个一个字节的读取并写入
            while(is.read(bts)!=-1)
            {
                os.write(bts);
            }
           os.flush();
           os.close();
           is.close();
          
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long  endTime=System.currentTimeMillis();
        System.out.println("采用流上传的方式的运行时间:"+String.valueOf(endTime-startTime)+"ms");
        return "/success"; 
    }

Simple file upload page

How to upload files in java

The above is the detailed content of How to upload files in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn