FileUpload java saves files in an improper folder

1

I found an example of fileupload on the internet and added it to my project. However, it uploads the files to a folder inside the glashfish server. This folder is named /config , and all the way I put it to create it, it creates only within that config folder. But the application stays in another folder called Aplications .

How do I get it uploaded to the webcontent folders of the project when it uploads the file? The path is set to the variable path_to .

package model;

import java.io.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import javax.servlet.http.Part;

public class FileUpload {

    private final int limit_max_size = 10240000;
    private final String limit_type_file = "gif|jpg|png|jpeg|pdf|doc|docx|txt";
    private String path_to = "";

    public FileUpload() {

    }

    public String processUpload(Part fileUpload) {
        String fileSaveData = "noimages.jpg";

        try {

            if (fileUpload.getSize() > 0) {
                String submittedFileName = getFilename(fileUpload);
                if (checkFileType(submittedFileName)) {
                    if (fileUpload.getSize() > this.limit_max_size) {
                        FacesContext.getCurrentInstance().addMessage(null,
                                new FacesMessage(FacesMessage.SEVERITY_INFO, "File size too large!", ""));
                    } else {
                        String currentFileName = submittedFileName;
                        String extension = currentFileName.substring(currentFileName.lastIndexOf("."),
                                currentFileName.length());
                        long nameRadom = Calendar.getInstance().getTimeInMillis();
                        String newfilename = currentFileName;

                        fileSaveData = newfilename;
                        String fileSavePath = path_to;

                        try {
                            byte[] fileContent = new byte[(int) fileUpload.getSize()];
                            InputStream in = fileUpload.getInputStream();
                            in.read(fileContent);

                            File fileToCreate = new File(fileSavePath, newfilename);

                            File folder = new File(fileSavePath);
                            if (!folder.exists()) {
                                folder.mkdirs();
                            }
                            FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
                            fileOutStream.write(fileContent);
                            fileOutStream.flush();
                            fileOutStream.close();
                            fileSaveData = newfilename;
                        } catch (IOException e) {
                            fileSaveData = "noimages.jpg";
                        }

                    }

                } else {
                    fileSaveData = "noimages.jpg";

                }

            }
        } catch (Exception ex) {
            fileSaveData = "noimages.jpg";

        }
        return fileSaveData;
    }

    public String getPath_to() {
        return path_to;
    }

    public void setPath_to(String path_to) {
        this.path_to = path_to;
    }

    private String getFilename(Part part) {

        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");

                return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1);

            }
        }
        return null;
    }

    private boolean checkFileType(String fileName) {
        if (fileName.length() > 0) {
            String[] parts = fileName.split("\.");
            if (parts.length > 0) {
                String extention = parts[parts.length - 1];
                return this.limit_type_file.contains(extention);
            }
        }
        return false;
    }
}
    
asked by anonymous 21.01.2017 / 16:21

0 answers