0

i am developing a java project and i have a dynamic web project under eclipse that receive requests and handle file upload , right now all is ok but i want to store the files uploaded somewhere to be able to host my files, for exemple, i want that when i tape this sample url :

localhost:18080/Uploader/img.jpg

in the browser i can see the image in the screen. this is what i have as project structure : project structure

i have a REST RESOURCE and i am not working with servlet level , and my application is deployed under wildfly not tomcat,

here my Rest resource to show you the code :

package rest;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.faces.bean.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import sun.misc.IOUtils;

import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

@RequestScoped
@Path("uploader")
public class UploadResource {

private final String UPLOADED_FILE_PATH = "d:\\";
@POST
@Path("/upload")
@Consumes("multipart/form-data")
@Produces(MediaType.APPLICATION_JSON)
public UploadResponse uploadFile(MultipartFormDataInput input)
        throws URISyntaxException {

    String fileName = "";
    UploadResponse uploadResponse = null;
    Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
    List<InputPart> inputParts = uploadForm.get("uploadedFile");
    for (InputPart inputPart : inputParts) {
        MultivaluedMap<String, String> header = inputPart.getHeaders();
        fileName = getFileName(header);
        // convert the uploaded file to inputstream
        InputStream inputStream;
        try {
            inputStream = inputPart.getBody(InputStream.class, null);
            byte[] bytes = IOUtils.readFully(inputStream, 0, false);                
            fileName = UPLOADED_FILE_PATH + fileName;
            uploadResponse = new UploadResponse(fileName);
            writeFile(bytes, fileName);
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return uploadResponse;
}

private void writeFile(byte[] content, String fileName) throws IOException {

    String url = this.getClass().getResource("/slide-1.jpg").toString();
    System.out.println("url" + url);

    /*
     * URL url = this.getClass().getResource("/"); System.out.println("url"
     * + url); File fullPathToSubfolder = new File(url.toURI())
     * .getAbsoluteFile(); String projectFolder =
     * fullPathToSubfolder.getAbsolutePath() .split("target")[0]; fileName =
     * projectFolder + "WebContent/uploads/resources/";
     */

    File file = new File(fileName);
    if (!file.exists()) {
        file.createNewFile();
    }

    FileOutputStream fop = new FileOutputStream(file);

    fop.write(content);
    fop.flush();
    fop.close();

}

private String getFileName(MultivaluedMap<String, String> header) {

    String[] contentDisposition = header.getFirst("Content-Disposition")
            .split(";");

    for (String filename : contentDisposition) {
        if ((filename.trim().startsWith("filename"))) {

            String[] name = filename.split("=");

            String finalFileName = name[1].trim().replaceAll("\"", "");
            return finalFileName;
        }
    }
    return "unknown";
}

}

please help me i am confused about this issue.

majd hwas
  • 95
  • 2
  • 2
  • 10
  • i have edited my question , can you help me please ? – majd hwas Mar 18 '16 at 14:53
  • Begin with @BalusC if you want to let him notified by your comment. – The Bitman Mar 22 '16 at 22:39
  • The duplicate still applies. You're attempting to save uploaded file in CWD / deploy folder. This is not going to work for reasons elaborated in the duplicate. You really need a fixed/absolute path outside deploy folder. The duplicate shows several ways to configure that path. One of them is `jboss.server.data.dir` which is really a great feature of JBoss servers. Make use of it (thank you for notifying, @TheBitman) – BalusC Mar 22 '16 at 22:53
  • I just want to help to @Majd Hwas because he addressed him question to nobody. An I think despite your scoff it was successful. – The Bitman Mar 22 '16 at 23:07
  • @BalusC And now I've forgotten to address my message to you! :D – The Bitman Mar 22 '16 at 23:37

0 Answers0