com.bookselling.util.FileUtilities.java Source code

Java tutorial

Introduction

Here is the source code for com.bookselling.util.FileUtilities.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookselling.util;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author Phan Phat
 */
public class FileUtilities {
    public void write(UploadedFile multipartFiles[], String path) {
        if (multipartFiles != null && multipartFiles.length != 0) {
            for (UploadedFile multipartFile : multipartFiles) {
                String fileWithFullPath = path + "/" + multipartFile.getName();
                File file = new File(fileWithFullPath);
                try {
                    FileUtils.writeByteArrayToFile(file, multipartFile.getData());
                } catch (IOException ex) {
                    Logger.getLogger(FileUtilities.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void delete(String path) {
        File file = new File(path);
        file.delete();
    }

    public static File multipartToFile(MultipartFile multipart) {
        try {
            File convFile = new File(multipart.getOriginalFilename());
            multipart.transferTo(convFile);
            return convFile;
        } catch (Exception ex) {
            return null;
        }
    }
}