fileuploadutil.java

来自「基于java的组态软件。使用了hibernate和spring技术」· Java 代码 · 共 52 行

JAVA
52
字号
/*
 * Created on 2005-8-9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package web.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * <p>Title: FileUploadUtil.java</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: SCUT Copyright (c) 2005</p>
 *
 * <p>Company: KHC</p>
 * 
 * @author yukizh
 * 
 * @version 1.0
 */
public class FileUploadUtil {
    public static void upload(String uploadDir,CommonsMultipartFile file) throws Exception{
        File dirPath = new File(uploadDir);

        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        InputStream stream = file.getInputStream();
        if((new File(uploadDir + file.getOriginalFilename()).exists())){
            return;
            //throw (new Exception(""));
        }
        OutputStream bos = new FileOutputStream(uploadDir + file.getOriginalFilename());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];

        while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        bos.close();
        stream.close();
    }
   
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?