Save image on server - image

I have a BufferedImage in my jersey post call and would like to save it on the server. I can save it to the file system but I want to save it to the server img folder.
EG localhost:8080/server/img
How do I achieve this?

Use the getRealPath() method of the ServletContext ServletContext.getRealPath to find out the actual directory in your filesystem and save it there. In your case it can be like
String realPathOfImgFolder=req.getServletContext().getRealPath("/img");
Then save the file to this location and it will be available by http://localhost:8080/server/img/<image file name with extension>

Related

Laravel putFile method error file does not exist but putFileAs works fine

I want to download and store some icon image files. first of all I tried using Storage::putFileAs method. It downloads the file normally but there is a problem with this method as it doesn't add the file extension to file name and I don't know how can I get the file extension before using this method. But I tried using putFile to see if it will guess file extension but it gives me error below on same url.
Symfony/Component/HttpFoundation/File/Exception/FileNotFoundException with message 'The file "https://assets.coingecko.com/coins/images/780/thumb/bitcoin_cash.png?1547034541" does n ot exist'
I used tinker framework to test this method before using in my project.
So my main question is why putFile doesn't stream the file whereas putFileAs does.
and my second question is how can I download and save an image file with specific file name without need to find the file extension in Laravel?
Storage::putFile('photos', "https://assets.coingecko.com/coins/images/780/thumb/bitcoin_cash.png?1547034541") // FileNotFoundException
Storage::putFileAs('photos', "https://assets.coingecko.com/coins/images/780/thumb/bitcoin_cash.png?1547034541",'test') // Finds the file but I don't know how to add file extention

Once Spring controller method is invoked can I confirm that the file is completely uploaded?

I am trying to upload an excel file in the OpenShift server data directory (OPENSHIFT_DATA_DIR) and store the data in the MySQL database provided by OpenShift. I am following the below steps:
Upload the excel file to the OPENSHIFT_DATA_DIR.
Read the excel file from OPENSHIFT_DATA_DIR and parse it.
Update/insert the parse data into the database
I am able to upload excel file into the OPENSHIFT_DATA_DIR with the below code. I use Spring's multipart resolver
private void uploadExcelFile(MultipartFile file) throws IOException{
String fileNameWithPath = System.getenv("OPENSHIFT_DATA_DIR")+"Test.xls";
file.transferTo(new File(fileNameWithPath));
//Start reading the excel file from OPENSHIFT_DATA_DIR
.......
.......
}
I will be able to read the excel file from OPENSHIFT_DATA_DIR, but my concern is I am not sure whether the file is completely uploaded or not at the time of reading the excel.
Once "transferTo()" method is executed can I confirm that the file is completely uploaded?
Or, How will I handle it if the file is not completely uploaded?
After the transferTo() mothod call, you can do anything you want, with the transferred file.
If you read the doc of the transferTo() method, you will see, it is not asynchron.
Documentation says:
void transferTo(File dest)
throws IOException,
IllegalStateException
Transfer the received file to the given destination file.
This may either move the file in the filesystem, copy the file in the filesystem, or save memory-held contents to the destination file. If the destination file already exists, it will be deleted first.
If the file has been moved in the filesystem, this operation cannot be invoked again. Therefore, call this method just once to be able to work with any storage mechanism.
Note: when using Servlet 3.0 multipart support you need to configure the location relative to which files will be copied as explained in Part.write(java.lang.String).
Parameters:
dest - the destination file
Throws:
IOException - in case of reading or writing errors
IllegalStateException - if the file has already been moved in the filesystem and is not available anymore for another transfer

How to map filesystem path to http url in spring

I'm working on a spring web mvc project which allows users to upload files. I am saving these uploaded files out of application context so that they persist across deployments. Saving file is working fine. I want to know the best way to convert the file system path to HTTP url so that it can be saved in the database and also used in HTML resource like etc.
Thanks in advance.
As you want to access a file in the filesystem like a static resource using spring mvc, the answer (taken from here) is to serve the static resources adding an entry like in your servlet context:
Example:
<mvc:resources mapping="/images/**" location="file:/absolute/path/to/the/resource/folder/" />
In this case you store all the files in the same path in the same server using only one resource, then you could only store in the db the filename.
In your html tag you need to use a relative url (being aware of your context, so you could access your file like http://yourhost:port/context/resource/yourfile).
In case you want to store files in a different server then you should add another resource origin (but it must be available as a file system path to the other server), so in that case it would make sense to store in the database a value like "resourcename/filename".

octopress-avatar: where is the avatar image file?

The path to the avatar is not described.
Where should be stored the avatar in order to use this plugin.
in source/images ?
You can put a url straight in the config file:
avatar_url: http://www.foo.com/avatar.jpg
so you might as well upload the image in your directory and use the full url for it in the config file.
avatar_url: http://www.foo.com/images/avatar.jpg

using an image embedded in a directory of a dll

I have a .NET 4.0 class library with a directory called Resources, with an image called Logo.bmp inside it set to be compiled as an embedded resource.
In my main application I add the dll reference and set a Uri to pack://application:,,,/ResourceImages;component/Resources/logo.bmp and then I try to get the resource stream to that resource (using Application.GetResourceStream(myUri)) but it can't find the resource specified.
If however I put the image in the root directory of my dll and take out the Resources/ it can find and return the resource stream without issue.
Any suggestions?
to anyone else who might be having this particular issue, make sure that you build the string to pass into the new uri BEFORE you make the new call, not during. I changed it so that the pack: location string is all created ahead of time and now it works

Resources