java development setting path in linux - java

i have development a simple webservices to upload a image to computer in linux. it have some problem the saving file location. when i summit the image, it become no response And i already import all require package.
#Path("/files")
public class V1_status {
/**
* Upload a File
*/
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(
#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail){
saveToDisk(uploadedInputStream, fileDetail);
return"File uploaded successfully!";
}
// save uploaded file to a defined location on the server
private void saveToDisk(InputStream uploadedInputStream,FormDataContentDisposition fileDetail
) {
String uploadedFileLocation= "/home/fairlady/Pictures" +fileDetail.getFileName();
try {
OutputStream out= new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out= new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

In this line
String uploadedFileLocation= "/home/fairlady/Pictures" +fileDetail.getFileName();
you are missing a forward slash after Pictures
try
String uploadedFileLocation= "/home/fairlady/Pictures/"+fileDetail.getFileName();

Related

Multipart form request in java REST API gives FileNotFound exception

#POST
#Produces("application/json")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response signUp(
#FormDataParam("name") String name, #FormDataParam("mobile") String mobile,
#FormDataParam("password") String password, #FormDataParam("email") String email,
#FormDataParam("dob") String dob, #FormDataParam("address") String address,
#FormDataParam("otp") String otp, #FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail,
#FormDataParam("path") String path) {
Registration reg = new Registration();
reg.setName(name);
reg.setMobile(mobile);
reg.setPassword(password);
reg.setEmail(email);
reg.setDob(dob);
reg.setAddress(address);
reg.setEnteredDate(new Date());
reg.setOtp(otp);
//path=http://myip:8080/project_name
String uploadedFileLocation = path + fileDetail.getFileName();
System.out.println(uploadedFileLocation);
writeToFile(uploadedInputStream, uploadedFileLocation);
RegistrationServiceI regService = new RegistrationService();
String result = regService.saveRegistration(reg);
JSONObject jsonObj = new JSONObject();
jsonObj.put("result", result);
return Response.status(200).entity(jsonObj.toString()).build();
}
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Everything working good. But when I am trying to save file, it gives me FileNotFoundException. I saw many questions related to this but I have not seen any answer in any website. Please help me.
Change FormDataContentDisposition to Multipart

Using relative path to store the uploaded file in java using Jersey

I am able to save image file in c://temp/images locally. But I want it in Project folder itself. Here is my code
#Path("/files")
public class FileUpload {
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "C://temp/images" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = uploadedFileLocation;
return Response.status(200).entity(output).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
When I tried to replace file location with /resources/Images/,I am getting file not found exception.

Nullpointer exception soapui file upload call

I have to upload a file using soap ui.Below is my service code.It executes fine if call using jersey.But when i try to call using soapui null pointer exception occurs.
I call the fileupload service in soap ui like below
Create Rest Project:
Add the Url:
http://localhost:8080/FileService/Services/HomeService/testupload
file file:c:\\1.wav
#POST
#Path("testupload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
public String uploadFile(#FormDataParam("file") InputStream fis,
#FormDataParam("file") FormDataContentDisposition fdcd) {
OutputStream outpuStream = null;
String fileName = fdcd.getFileName();
String filePath = FOLDER_PATH + fileName;
try {
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(filePath));
while ((read = fis.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch(IOException iox){
iox.printStackTrace();
} finally {
if(outpuStream != null){
try{outpuStream.close();} catch(Exception ex){}
}
}
return "File Upload Successfully !!";
}
How to fix this issue? Any help will be greatly appreciated!!!

Local upload file via rest web service

I am using the code in this link http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ to upload a file.In this example I have to pass from a html page to specify the file to upload but I want to to acceed to it when I call the webservice by its path ( s.thing like that : http://*****:8080/RESTfulExample/file/upload/C://image.png)
Are there any suggestions to this issue? Please help!
That is what i did till now to solve it
#Path(value="/files")
public class upload {
#POST
#Path(value = "upload/{path}")
#Consumes("image/jpg")
public Response uploadPng(#PathParam("path") String path, File file) throws IOException {
file = new File("path");
String uploadedFileLocation = "C:/Users/Desktop/" + file.getName();
DataInputStream diStream =new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead =
diStream.read(fileBytes, read,fileBytes.length - read)) >= 0) {
read = read + numRead;
}
writeToFile(diStream, uploadedFileLocation);
System.out.println("File uploaded to : " + uploadedFileLocation);
return Response.status(200).entity(file).build();
}
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out =new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}}}
But I have an 405 error now !!
EDIT
#Path(value= "/up")
public class upload {
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://Users/Marwa/Desktop/mafile.png";
#POST
#Path(value="upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(#FormDataParam("file") InputStream fileInputStream) {
String filePath = SERVER_UPLOAD_LOCATION_FOLDER ;
System.out.println("*****serverpath********");
saveFile(fileInputStream, filePath);
String output = "File saved to server location : " + filePath;
return output;
}
private void saveFile(InputStream uploadedInputStream,String serverLocation) {
try {
OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);}
outpuStream.flush();
outpuStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think you'd just call http://example.com/file/upload and post the file there with the browser (with JavaScript) or some other client. For example, you could test it with curl
curl -i -F "file=#/home/user1/Desktop/test.jpg" http://example.com/file/upload
Do you need the file path for something on the server side? If you need the path on the server side for some reason, you could just add a #PathParam.
#POST
#Path("/upload/{path}")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#PathParam("path") String path,
#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail) {
...
}
You could also try leaving off #consumes or using a specific type like #consumes("image/jpg"). For example:
#POST
#Path("/upload/{path}")
#Consumes("image/jpg")
public Response uploadFile(
#PathParam("path") String path,
InputStream uploadedInputStream) {
...
}

How can I get the actual uploaded file in the server side?

I am using REST API with JAX-RS,
I just upload the file and my server code is as follows,
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
#Path("/upload")
public String uploadFunction(#Context UriInfo uriInfo,
#FormDataParam("upload") final InputStream inputStream,
#FormDataParam("upload") final FormDataContentDisposition fileDetail) {
//Here I want to get the actual file. For eg: If i upload a myFile.txt. I need to get it as myFile.txt here
}
My code is working correctly when I parse the content of the file using inputStream and performed some operation. Now I want the exact file. Since I need to send mail with the actual file attached
Here I want to get the actual file. For eg: If i upload a myFile.txt. I need to get it as myFile.txt here. How can I achieve it?
I might be wrong here, but when using the InputStream you can only get the inputstream, because the file is not stored on the server yet.
So in that case you should be able to do something like the following:
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "/somepath/tmp/uploaded_files/";
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.TEXT_PLAIN)
#Path("/upload")
public String uploadFunction(#Context UriInfo uriInfo,
#FormDataParam("upload") final InputStream inputStream,
#FormDataParam("upload") final FormDataContentDisposition fileDetail) {
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + fileDetail.getFileName();
// save the file to the server
saveFile(inputStream, filePath);
String output = "File saved to server location : " + filePath;
return Response.status(200).entity(output).build();
}
private void saveFile(InputStream uploadedInputStream, String serverLocation) {
try {
OutputStream outputStream = new FileOutputStream(new File(serverLocation));
int read = 0;
byte[] bytes = new byte[1024];
outputStream = new FileOutputStream(new File(serverLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Categories

Resources