Upload Image using Java is not saving properly in the cloud - java

My form has an upload button for the user to save file images.
ServletContext context = ServletActionContext.getServletContext();
String appPath = context.getRealPath("");
String filePath = appPath+"\\images\\categories";
File fileToCreate = new File(filePath, getMyFileFileName());
FileUtils.copyFile(getMyFile(), fileToCreate);
I need to upload the picture and put it on the image/categories folder for future access. It is working well in my localbuild but when I deployed it on the cloud by uploading the .war file, it doesn't work anymore. It cannot find the image.
HTML Error upon accessiing the image is as follow
type Status report
message /Teapop/images/categories/team.png
description The requested resource (/Teapop/images/categories/team.png) is not available.
What am I doing wrong?

please try to use File.separator instead
String filePath = appPath+File.separator+"images"+File.separator+"categories";
and please give me some feedback
Hope that helps .

Related

Unable to get specific Drive File thumbnailLink in Android Studio with Drive Api

I am trying to get a file thumbnailLink through the Drive Api, in android studio but keep getting errors. The authorization and Drive Service is working just fine and I'm able to get the file name and create files. However when I try to get the file's thumbnail link the error message is
I/error:: getting thumbnail: println needs a message
This is my code right now, the Log to get the file's name works perfectly
public Task<File> getThumbnail(String fileId) {
return Tasks.call(mExecutor, () -> {
Log.i("chegou", "thumbnailLink");
final File file = mDriveService.files().get(fileId).execute();
Log.i("file name", file.getName());
Log.i("file thumbnail", file.getThumbnailLink());
return null;
});
}
Also, I tried to get the thumbnail from the exact same file in the google playground and it worked perfectly
Does anyone has an idea of how to solve it? Thanks!
So, I just dicovered the answer! Apparently there's a permanent link to every thumbnail in drive, you just need the file id
It is: https://drive.google.com/thumbnail?authuser=0&sz=w320&id=[fileid]
EDIT:
Also, I just found out that when you list files if you put in setFields thumbnailLink you can also get a temporary thumbnailLink
Here's an example code:
FileList searchList = mDriveService.files().list()
.setQ("name = '" + fileName + "'")
.setSpaces("drive")
.setFields("files(id, name,size,createdTime,modifiedTime,starred,thumbnailLink,mimeType)")
.execute();
then you just need to get the file from the list to get it's thumbnailLink
searchList.getFiles().get(0).getThumbnailLink()

Upload image using data URI to server with Java (PlayFramework-2.6 or Plain Java)

I am passing an image from a webpage using AJAX request. The image is passed as a Data URI to a Java function as shown below.
public Result upload() {
String dataUri = request().body().asText();
System.out.println(dataUri);
File file = dataUri;
return ok("File uploaded");
}
The problem I am having is I think I need to convert the data URI to a file object in order to pass it into "File file = ". And if I can do that, then the image will be uploaded to the server. Any takers?
Cropped and uploaded! I found another link on stackoverflow that showed me how to convert base64 image to file. I have successfully loaded the cropped file to a directory on the server. Thanks for your help!! The link which has the answer is here:
Convert base64 string to image

I want to store image in WebContent\upload\news folder and save path in database.in servlet in eclips

[My code is:]
Coding:
String savePath = getServletContext().getRealPath("/")+"upload/news/";
File fileSaveDir=new File(savePath);
if(!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
Part part=request.getPart("uploadFile");
String fileName=extractFileName(part);
part.write(savePath+fileName);
I used this code image is store in .metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\RealEstate\upload\news
Please suggest me how can store image on WebContent\upload\news folder
Thank you.....

Changed Image file is not getting displayed using struts2

I am tring to upload a file using struts2.File uploading is successful.I tried out many hints from stack overflow,but still failed to find a solution for this.
Nw when i try to change the image,the new image appears only after clearing the cache.It does not change spontaneously.The struts2 framwork makes use of tiles and
the images are getting displayed on header and leftContainer.
The file is named after the memberId logged in..(which is stored in session)
I made use of fileUpload code from:
http://www.tutorialspoint.com/struts_2/struts_file_uploads.htm
destPath = org.apache.struts2.ServletActionContext.getServletContext().getRealPath("/");
destPath = destPath + "images";
//where images are stored in images folder of webapps..
When i try uploading a new image,this image will be renamed by the memberId of the loggedin member(stored in session).
What my exact problem is that, new file is getting successfully uploaded to the images path.It is also renamed correctly.But this new image appears only when i either refresh the page or clear the cache.Why is this so??
How can refreshing be avoided?
Please help..........

upload the file by using path

Is it possible upload file through jsp.
var filepathhere = "http:// xxxx.com/--------/upliaded.pdf".
for exm: http:// xxxxx/test.jsp?file = filepathhere.
can i uses like this, is it possible to by using the path we can upload this.
Accept URL from user in HTML form.
POST URL to servlet , try to read File from passed URL check its type and other validations.
Read file from that URL and on write or insert it in to your own DB
File Upload and Download using Java
Yes you can. request.getParameter("file") return Object that you can cast it to String and can download from that URL
something like that :
String file= (String) request.getParameter("file");
URL fileUrl = new URL(file);
and can download from here
You can get the information in the format of string doing it this way but I don't think you can get any other format( like pdf in your example) of data directly from the url. You can off course first download the file to some path and then get it from there.

Categories

Resources