I want to programm a messenger, which runs on a Server and can send an zip file at any size. So you would send the zip file to the server and when any other person is online, the server sends the zip File to the other person. I have no idea what servertype I could use and when I was reserching I found nothing. Because I want to run the sever on my Raspberry Pi, it would be also helpfull, if I could slow the datastream down. It would be very helpful, if one of you could recommend servertypes, classes or methods, so that I can do more research.
Thank you in advance
You can create a maven-project on EclipseIDE and use Jersey for API-layer (tutorial: https://www.journaldev.com/498/jersey-java-tutorial).
Your class:
#Path("/zip")
public class ZIPRest{
#POST
#Path("/upload")
#Consumes("application/json")
#Produces("application/json")
public Response uploadZIP(InputStream file){
// TODO: convert InputStream in BLOB and save it on database
}
}
Related
can you please help on below points to understand Jersey flow.
Considering below example code
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces({ MediaType.APPLICATION_JSON })
public CustomObject acceptFile(#FormDataParam("json") FormDataBodyPart json,
#FormDataParam("data") FormDataContentDisposition contentDisposition,
#FormDataParam("data") final InputStream input){
...
}
if I transfer file of 20 MB, control does not get into above method until all 20 MB data is transferred to server from client. this is my observation. am I correct?
Does Jersey creates temporary files or stores file contents in memory?
if Jersey creates temporary files where does these temporary files being created?
For completeness I tried to use HTML as client.
I am prototyping a very simple POST service to upload a file:
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("fileUpload")
public Response uploadFile(MultipartBody body) {
//never gets to here
System.out.println("In uploadFile");
//TODO: implementation
return Response.ok().build();
}
I get
org.apache.cxf.interceptor.Fault: Couldn't determine the boundary from the message!
I also tried to replace the method declaration with:
public Response uploadFile(List<Attachment> attachments,
#Context HttpServletRequest request) {
as per some Google findings, to no help.
I am using two different clients to invoke this service: chrome://poster, which has a field to include a file attachment, and a simple Python script as is explained here. Both clients produce the same result.
How should I change my service implementation or the call or both in order to be able to pass the CXF validation and enter into the actual body of the service?
REFERENCES:
JAX-RS : Support for Multiparts
Apache CXF: JAX-RS Restful web service for uploading/downloading Text file + Java client
The server side code looks fine. Problem is the way you are sending data from client. You are sending data as a stream in payload not as an attachment which has boundary. To verify quickly you can enable logging request and response by enabling CXF Feature LoggingFeature or Interceptors LoggingInInterceptor and LoggingoutInterceptor. In the log if you see data coming Payload then you are sending data as stream in this case you need to change the way you send the data else you can change consumes to application/octetstream and receive data using inputstream directly.
I'm not aware of the tool you are using, however I use Chrome Extension to postman to test the REST services. If you install the extension and launch the application.
You can upload the file using below approach.
Change Method type to POST from the drop down.
Enter the URL
Select Tab Body
Select Form-Data Radio Button
On the right most row select file from drop down. as shown in diagram.
Choose file to upload.
Optional enter multipart key.
Finally click send.
We can reproduce your error by selecting binary radio button and uploading file as shown below.
I'm working on android app and back-end server Rest API part.
I'm at the point where i need to return some video files from the server back to my android device. How can i do that?
I looked up jersey documantation https://jersey.java.net/documentation/1.19/jax-rs.html#d4e142
and http://www.vogella.com/tutorials/REST/article.html#restjersey_annotations
but din't have any luck figuring this out..
For images i've been using the
#Produces(image/jpg)
Is there a similar way i can do to share mpeg4 or any other video files?
What would be the best approach there?
As android client can stream the video content, try something like this
#GET
#Path("video")
#Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response video() {
File file = new File("C:/Data/video.mp4");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.build();
}
Hi ive been having some trouble trying to transfer a png image to my webserver using java and php Ive tried using FTP but the software that Im scripting for blocks port 21 rendering it useless
I was directed to use form urlencoded data then use a POST request to get it
im completely lost on this topic and could just use some direction apparently file and image hosting sites use the same method to transfer files and images from the users computer to their servers.
maybe just an explanation of whats going on might help so that I can grasp what exactly im trying to do with java and php
Any help would be much appreciated!
I've also been facing the same kind of problem a short time ago.
After some researches, I found out that the HttpComponents library from Apache (http://hc.apache.org/) contains pretty much everything you'll need to build HTTP-POST request in a quite simple way.
Here is a method that will send a POST request with a file to a certain URL:
public static void upload(URL url, File file) throws IOException, URISyntaxException {
HttpClient client = new DefaultHttpClient(); //The client object which will do the upload
HttpPost httpPost = new HttpPost(url.toURI()); //The POST request to send
FileBody fileB = new FileBody(file);
MultipartEntity request = new MultipartEntity(); //The HTTP entity which will holds the different body parts, here the file
request.addPart("file", fileB);
httpPost.setEntity(request);
HttpResponse response = client.execute(httpPost); //Once the upload is complete (successful or not), the client will return a response given by the server
if(response.getStatusLine().getStatusCode()==200) { //If the code contained in this response equals 200, then the upload is successful (and ready to be processed by the php code)
System.out.println("Upload successful !");
}
}
In order to complete the upload, you must have a php code that handle that POST request,
here it is:
<?php
$directory = 'Set here the directory you want the file to be uploaded to';
$filename = basename($_FILES['file']['name']);
if(strrchr($_FILES['file']['name'], '.')=='.png') {//Check if the actual file extension is PNG, otherwise this could lead to a big security breach
if(move_uploaded_file($_FILES['file']['tmp_name'], $directory. $filename)) { //The file is transfered from its temp directory to the directory we want, and the function returns TRUE if successfull
//Do what you want, SQL insert, logs, etc
}
}
?>
The URL object given to the Java method must point to the php code, like http://mysite.com/upload.php and can be build very simply from a String. The file can also be build from a String representing its path.
I didn't take the time to test it properly, but it was build upon proper working solution, so I hope this will help you.
I want to send a file to the Browser via the REST Interface.
Can you suggest the most efficient way to do it, Keeping in mind the following?
Not much traffic.
I am fetching the file from HBase which means when I fetch it from HBase I get it in Byte Array.
The files are not in any folder in the server. The files can only be fetched from the HBase table.
The Front end is PHP and I do not know PHP.
In the REST api you can just pass the byte array to Response and it takes care of itself.
Using the following code -
#Produces("image/jpg")
public Response getImage() {
<Fetch it from where ever you have it>
Response.ok(<byteArrayOfTheFile>).build();
}
I am giving case study of WebService by which i send file:
It is always good to encode the file content and send it to the destination where they will be decode it and read the content.
Sending as an attachment is always open to the world becasue it is not encrypted.And if the network having high trafic chances of failure is high.