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.