Send MultipartFile with additional options - java

what would be the best way to transfer a file from one server to another ? On one server, the file is stored in the database, you need to pull it out of the database (its bytes, name, type) and send a post request to another server and write it there.
I tried to do it in the form of json (the bytes were translated into base64, but the file turned out to be incorrect).
Need to transfer the file and plus some fields with it. Also, the file may not be one, but an array.
How to implement?
can you show a complete example

Related

Sending a Java Class Object as an email attachement

I'm using Java's email class to send an email through our SMTP server. I want to add a feature where if a user resubmits their form, I email them an attachment with a copy of the old data and their new data they are submitting, so they can see the changes they are making. I don't have a problem with this flow, it's when I want to add them to my email I run into issues. Most of the solutions I've seen for adding attachments to a java email consist of attaching a system file, but these objects are generated at runtime, and I don't want to start adding files to the system just for this feature, so I wanted to know if I can add objects specifically as attachments.
If you were going to output the object to the console for debugging purposes, how would you do that? Would you .ToString() it, would you iterate over the properties and output them as individual strings?
Rather than think about the object as an object, think about how you want to show it to your users. If this involves a lot of rows with descriptors so they know what the data mean, then output it like that. If they are developers and expect to see it as JSON, output it formatted like that.
Most likely, you'll have to iterate over the object in some way to output it as strings in the body itself.
If you really want to add an attachment, you can create a temporary file (with a random name to prevent overwriting the file), send the email with the attachment, then delete the file.
I was going to say that there's probably a way to create a file in memory without saving it to disk that you can use as an attachment, but maybe there isn't in Java.
Create a File object in memory from a string in Java

Downloading a file from MySQL using Java EE and primefaces

Well I have a database with a table that contains a BLOB field, the thing is that I need to make a link to download the files.
So far in my backing beans I have a method to generate each record with its respective file, and a method that reads the array of data.
When I use the P:Datatable to show the records I can view every single record, name and file accesing the file.methods in each one.
But I cannot find a way to make it downloadable, I has been searching the forum and the network for away to make something like this
(listadoArticulos.listado is the array of articles, each article contains a few String fields and a single blob)
I need to link to the file itself so the user can download it.
In my backing beans I need to make a method so I can link it, the method has to pass the "File" in the database to bytes (I guess)
I will apreciate any help or direction
You don't use JSF to generate the file download. The file download is done directly by a Java handler/servlet, which will set the content-type directly and stream the file content as bytes.

Methods for storing data in java

In my software I want to store some data, that later they will be used. Something like a database to hold data:
Date, source path, destination path, and an array of file names.
Also another table to hold information about ftp connection:
Host, port, username and password
I need to know what methods are available to store and parse these data. I noticed there is a file type called .csv, is this an option for me? And is there any other option?
I think this depends a lot on how much data you want to store and how you need to access it.
If your application is going to be collecting a lot of structured data, such as user profiles, or product information, ie, if your application is all about a database then, yes as others have commented some sort of SQL database would make sense.
If your needs are more along the lines of just storing some "session" information, maybe like the last state of a GUI form for example, you might want to just serialize the data and write it to a simple text file.
One simple way to do that would be to serialize the data in a human readable format such as JSON and then write the text to a file, and then read it back and deserialize it when you need to restore it from storage.
If this is what you are looking for take a look at gson (from google), it provides a very easy what to convert a java object to JSON and back again.
JSON, is just text, so you can just read and write it to a simple text file.

How preserve and keep timestamp of original modified date for file when uploading to Amazon S3?

Is there any way either in Java or using s3cmd to keep and preserve the modified date on the file as opposed to the LastModified Amazon sets with sysdate when you actually upload the file to S3 ?
Amazon S3 seems to use the upload timestamp as the LastModified on the file, and not the original file date/timestamp.
Thanks.
Remember, S3 isn't storing "files" as you think of them on a filesystem. You're giving it a sequence of bytes for it to store. The source of that data can be a file, but it could also be a random stream of data generated by any data source. That data can have metadata. One piece of metadata that comes back in the GET Object request is Last-Modified, and S3 decides this for you.
But there's no reason you can't add your own custom headers and preserve whatever metadata you'd like:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
Specifically see "x-amz-meta-". You can have something like x-amz-meta-what-i-think-last-modified-should-be
This would really be a suggestion for Amazon, I would post to their forums. Alternatively, you could write a simple program in Java which would add a timestamp corresponding to your last modified time to the file. After downloading back from Amazon you could run the app in reverse to retrieve that information. Hope this helps.

Need to send multiple objects through an http output stream

I am trying to send some very large files (>200MB) through an Http output stream from a Java client to a servlet running in Tomcat.
My protocol currently packages the file contents in a byte[] and that is placed a a Map<String, Object> along with some metadata (filename, etc.), each part under a "standard" key ("FILENAME" -> "Foo", "CONTENTS" -> byte[], "USERID" -> 1234, etc.). The Map is written to the URL connection output stream (urlConnection.getOutputStream()). This works well when the file contents are small (<25MB), but I am running into Tomcat memory issues (OutOfMemoryError) when the file size is very large.
I thought of sending the metadata Map first, followed by the file contents, and finally by a checksum on the file data. The receiver servlet can then read the metadata from its input stream, then read bytes until the entire file is finished, finally followed by reading the checksum.
Would it be better to send the metadata in connection headers? If so, how? If I send the metadata down the socket first, followed by the file contents, is there some kind of standard protocol for doing this?
You will almost certainly want to use a multipart POST to send the data to the server. Then on the server you can use something like commons-fileupload to process the upload.
The good thing about commons-fileupload is that it understands that the server may not have enough memory to buffer large files and will automatically stream the uploaded data to disk once it exceeds a certain size, which is quite helpful in avoiding OutOfMemoryError type problems.
Otherwise you are going to have to implement something comparable yourself. It doesn't really make much difference how you package and send your data, so long as the server can 1) parse the upload and 2) redirect data to a file so that it doesn't ever have to buffer the entire request in memory at once. As mentioned both of these come free if you use commons-fileupload, so that's definitely what I'd recommend.
I don't have a direct answer for you but you might consider using FTP instead. Apache Mina provides FTPLets, essentially servlets that respond to FTP events (see http://mina.apache.org/ftpserver/ftplet.html for details).
This would allow you to push your data in any format without requiring the receiving end to accommodate the entire data in memory.
Regards.

Categories