Send Mulitpart file from Controller to JSP (Spring) - java

My requirement is to upload a JPEG/PDF file & save it as BLOB.
We have done it. But if server side error occurs which redirects to JSP page, I will get all data (e.g. input fields, drop downs, checkbox, etc.) except file.
Then again I need to choose a file.
Is there any way to preserve a file or send a file from controller to JSP.

No it is not possible.
The simplest workaround is to keep the uploaded file in session so that you can recover it during the next form submission. Take care to users working with several tabs/windows: use a session key that clearly identifies the form on which the user is working. You could for example generate a unique identifier that you then store in a hidden field of the form.
To be able to download it again, you would need to provide second mapping that retrieves the file from the session.

MultipartFile inputFile = fileUploadBean.getFile();
HttpSession session = request.getSession();
if(!(inputFile.isEmpty())) {
session.setAttribute("inputFile", inputFile);
}
logger.info("inputFile : " + session.getAttribute("inputFile"));
if(inputFile.isEmpty() && session.getAttribute("inputFile")!=null) {
inputFile = (MultipartFile)session.getAttribute("inputFile");
}
This is what I did.

Related

Is there a way to retrieve file from SharePoint without file name?

I know how to retrieve a file with a specified name like this:
HttpGet httpGet = new HttpGet(siteUrl +
"_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/Working%20Files/FolderName')/Files('"
+workbookName+"')/$value");
but is there a way to retrieve it without the name? maybe by index?
Ive checked the documentation on https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest but saw no other examples on how else to retrieve files
Every file you upload into the Sharepoint will have the field Id mapped to it. This ID would be returned as part of the response whenever you create the file in the Sharepoint. This response would also return several links and fields related to the particular file you uploaded, in order to perform any operations on that file.
Using these fields such as Id, E-Tag you should be able to form another request to get the file name.
You can also find the request URL in the response you received after file creation.

How to run a jsp file by different url pattern other than filename.jsp?

I want to create a blogging like CMS in jsp.
So I have a "post.jsp" which will dynamically display the content.
I have a database which stores some url pattern and their content.
For example database stores:
url: www.example.com/abc
content: hello
url: www.example.com/xyz
content: world
I want to do that when a user will enter "www.example.com/abc" it will run "post.jsp" and check in database whether the url is present or not. If present then it will display "hello" else 404 error/
Similarly if user will enter "www.example.com/xyz" it will again run "post.jsp" and if the url present then it will display "world" else 404 error.
Every time the url should run "post.jsp" if no extension like html or jsp is present in the url pattern. If a url "www.example.com/contact.jsp" is entered by user then it should run contact.jsp without entering "post.jsp" because the url pattern contains a .jsp extension.
Hope you understand my requirement. How to do this?
I would create a mapping for 404 in my web.xml and define a JSP / servlet to be called whenever a URL is called that is not found. In that Java code I would do something similar to this (pseudo code !):
String request = getRequestUrl();
if( !request.endsWithIgnoreCase( ".jsp" ) && !request.endsWithIgnoreCase( ".htm") {
String tagToSearch = request.getEverythingAfterSlash();
String content = Db.searchFor( tagToSearch );
Response.write( content );
}
And please make sure that no-one is able to inject dangerous SQL by using this mechanism !
How this needs to be implemented depends quite a bit on which environment you use (Java version, Servlet version, Framework, Application server)

How to request a file and get it or get a message in case it cannot be created from the server

Using JQuery and Spring's #ModelAndView annotation for the controller.
I'm trying to code a process in which the user clicks an icon and if a certain criteria on the DB is met, a zip file will be produced on the server containing a bunch of files, then this zip file should be sent to the browser for saving.
If the criteria isn't met, then an error message should be sent to the browser telling there isn't any file to be created and produced.
However if I use JQuery' .post method, I can receive the error message (if that is the case) but never the zip binary file.
If I use a regular Href Link I can receive the file (if that is the case) but don't know how to receive the message when the file cannot be produced.
Is there an alternative or a standard way to do this?
Thanks for your support!
-Gabriel.
You should probably split your server-side method in two:
the first one validates the criteria. If unsuccessful, it notifies of an exception, otherwise it returns a URL to the method in next point
the second one actually returns the zip file
In your frontend, the code will look something like this:
$.post(urlToPoint1, data, function(response) {
if (response.success) {
// download the file using the url provided
// (pointing to method described in point 2)
window.location.href = response.url;
}
else {
alert('whatever');
}
});

request.getParameter not working

I have developed a file download program, which is implemented by Java Servlet.
The link sent to the user is in the following format:
http://examples.com/file?id=6565_1_1_1_201110
When user clicks this link, it will send a request to a servlet program to find the file name and then download the file. In the servlet I use the following code to retrieve the id value:
String param = "";
synchronized (this) {
param = request.getParameter("id");
}
Now the problem is, some users can get the correct value, which is 6565_1_1_1_201110. However, some users have obtained the wrong value, like 656543_1_1_1_20111067, etc, which is not fixed. I do not know where are those additional number from?
Any idea on this? Thanks.

how to save image in my sql in blob type columns with Struts2?

I have a Struts2 jsp page their i am sending one image, Temporary file path is comming to my java class after form submission but i do not know how can to save that path in db by changing it to Blob type.. Please consider this image columns is of blob type in my database table..
Here is the output what am getting in my Javaclass after the form submission:
My image path:
F:\Documents and Settings\software.netbeans\7.0\apache-tomcat-7.0.11_base\work\Catalina\localhost\AIGSA\upload__214d4f3e_136e8b74d9c__7fff_00000021.tmp 105542
filenames:
* Winter.jpg
Code:
for (File u: repImage)
{
System.out.println("*** "+u+"\t"+u.length());
}
int saveToDb= mo.addMembers(memberName, repImage);
How can I send my form Image to this {repImage Name, so that it will be easy to save it so my db
am not sure, but try:
int saveToDb= mo.addMembers( memberName, repImage.getBytes() );
If I understand your question properly, you want to store the binary data in your database. I'd say this is a bad idea in the first place for multiple reasons. A better method would be to reference a relative path that you can then use in your web application or the file system directly. Better yet, just store the reference to the location in Amazon/S3 where you want to save it/use it.
Regardless, to answer your question you would want to use ByteArrayOutputStream.

Categories