I am building a spring boot application in which, at a certain point, the user has to input a file (image, text or whatever he wants) and this file is eventually going to be auto-sent to me by mail. I have used this, in my HTML template, to ask for the output;
<div class="custom-file">
<input type="file" class="custom-file-input"
name="6?-BBtn9E#=G8Yua" id="6?-BBtn9E#=G8Yua"> <label
class="custom-file-label" for="6?-BBtn9E#=G8Yua">Select file</label>
</div>
As you can see I'm using bootstrap.
In my spring boot controller I'm getting that param this way;
#RequestParam(name = "6?-BBtn9E#=G8Yua", required = false) File userFile
Then the mail is going to be sent regularly (I've previously tested the mail-sending worked, when I still hadn't this file input issue), hence I'm just going to show you the attaching;
HtmlEmail email = new HtmlEmail();
email.attach(userFile);
email.send();
However, when I submit, I get this IOException in the console;
java.io.IOException: "/Users/username/Eclipse-Workspace/Website/file.txt" does not exist
I've picked up a file called file.txt, and since I'm running this on a mac computer the path is /Users/{myUsername}/{Eclipse-Workspace}/{Spring-Boot-Project/{file-name}.
What I want is that Spring Boot searches out of the project, not just inside of it.
Why is it doing so? Thanks for reading!
When you want Spring to look outside the project folder you want: "file:/Users/username/Eclipse-Workspace/Website/file.txt".
Related
Problem:
I have a number of pdf files saved in the resources folder of my web app. When I try to display the file in the jsp I am not able to. Below is the code snippet:
jsp file:
<input type="hidden" id="myResourceFile" value="<%=request.getServletContext.getRealPath("/resources/myattmts/")%>"/>
<div id="myrowid">
<embed TYPE="application/pdf" align="middle" width=100% style="height:400px" id="pdfDoc"/>
</div>
javascript code:
document.getElementById("pdfDoc").src=$('#myResourceFile').val()+"'\'"+filename;
Error:
The pdf does not load as the file source delimiter '\ is not evaluated correctly.
Any help where I am going wrong
I am testing the web application in my locally. However it is to be deployed over a particular node after build and release.
The problem here was that the pdf file is retrieval only from the application context inside a jsp.
However when I use request.getServletContext.getRealPath("/resources/myattmts/") it tries to access the actual war file in the tmp/deployment server (the actual physical location where the application is running and the file is present). And is not able to read it from there.
Instead I changed the value of the hidden variable "myResourceFile" in the jsp to the application context path as
jsp change:
<input type="hidden" id="myResourceFile" value="<%=request.getContextPath("/resources/myattmts/")%>"/>
So the file path now reads as "`http://localhost:8080/app-name/resources/myattmts/fileName.pdf"
instead of the actual physical path (in the previous case which was not accessible to the jsp)
as "E:/MyServer/deployments/tmp/app.war/resources/myattmts/fileName.pdf"
Using my company's chosen cloud storage api and just starting out with JSF 2.2. Trying to build a simple image upload web app and it works when I run locally using Eclipse.
Here's the code:
.xhtml page
<h:form id="form" enctype="multipart/form-data" prependId="false">
Select id Photo: <h:inputFile id="file" value="#{customerEntity.uploadedFile}"/>
<h:commandButton value="Query Cloud"
action="#{customerEntity.addActionController}"/>
</h:form>
customerEntity.java code:
private Part uploadedFile;
private String fileURI;
On local machine, code to extract file name from Part object uploadedFile produces correct file locator (eg. c:\pictures\mypix.jpg) for local access on my machine.
However, when I load into tomcat 7 running in a cloud vm, the application fails with a FileNotFound in the 'try' block:
File source = new File(this.fileURI);
try {
cloud.upload(new FileInputStream(source), source.length());
My debug statements show it's using the file locator from my local pc which clearly doesn't exist on the server. I can't figure out how to get the local file data streamed to the code running on the server.
As a slight jsf newbie, I'm sure it's something obvious, but can't figure it out or resolve via some of the other posts I've seen.
Any guidance would be greatly appreciated!
You're making a rather major conceptual mistake. You should not be interested in the client-specified path/name of the uploaded file. Instead, you should be interested in the content of the uploaded file. And well due to 2 main reasons:
The path to the file will be totally absent when you're using a webbrowser which doesn't expose a security bug that the full client side path is included in the name of the uploaded file (i.e. any browser other than Internet Explorer).
Using new File() on the client-specified path/name would only work if both the webbrowser (which is been used to send the file) and the webserver (which is been used to retrieve the file) runs at physically the same machine, because they have then access to exactly the same local disk file system structure. This doesn't occur in real world.
In order to save the content of the uploaded file in the desired location, do so:
uploadedFile.write("/path/to/uploads/somefilename.ext");
Note: you need to make sure that the somefilename.ext part is unique in its folder. You can if necessary make use of File#createTempFile() to autogenerate an unique filename in the given folder based on filename prefix and suffix.
File file = File.createTempFile("somefilename-", ".ext", new File("/path/to/uploads"));
uploadedFile.write(file.getAbsolutePath());
See also:
Recommended way to save uploaded files in a servlet application
I have a problem with uploading a large file (>max_upload_size). Why all form fields are null (when uploading this file) in form validate method?
I tried to manage this by asking if file is null and return action error if so, and while debugging seems ok, i get no response to my browser. I have already managed this on another project. This former project worked on Bea web server, while this current is on Jboss.
Can you post the what your jsp looks like.
Also do not forget to include this (enctype="multipart/form-data") in your jsp form definition:
<html:form action="/uploadme" enctype="multipart/form-data">
<html:file property="myfileToupload"/>
</html:form>
My site transforms an XML to HTML pages. Inside "Web Pages" folder I've create a folder "acces" that will contain the generated HTML pages and the images used inside. The generating process works, it places the files HTML and jpg, in the corect format in the folder acces. I can acces them from my local disk. When I try to acces the jpg at localhost:8080/myapp/acces/img/Image1.jpg, it works, but when I access localhost:8080/myapp/acces/img/someHtml.html it returns error 404.
If I open the file, from that folder, with that specific name: someHtml.html directly with a browser, it works.
What should I do to make the page become visible. I want to use it inside an HTML iframe.
I think what might be happening in your situation is that your Glassfish is likely deploying your application in a WAR archive, and what happens is your application might be writing to where your code is contained (wherever your workspace might be) so it won't be accessible from the application which is currently running off the WAR file you previously generated. Glassfish has likely already loaded up your application from the WAR file into memory and won't see the new files you've created until you rebuild and redeploy
What you might need to do is write it to some folder, and perhaps have a servlet that will retrieve the file and send it to client. That to me isn't the most elegant solution, you could just use a HTTP Server in front of the glassfish (apache or nginx or whatever) read the generated HTML file
Solved!
In my case I joust corrected:
<form action="mServlet" method="post">
<input type="text" name="variable1"/>
<input type="text" name="variable2"/>
<input type="submit" name="btnBoton"/>
</form>
It was misspeled:
miServlet --> mServlet
In my web application one of my pages is uploading a photo to the path
/usr/local/rac/picture-name-goes-here
The photo is uploading fine, but I need to access it in another page and when I try to access it from my JSP, it will not show up, I am guessing my path to the photo is incorrect
The code in my JSP to access the photo looks like the following.
<tr>
<td>
<img src="/usr/local/agent/photo-name-here.jpg"/>
</td>
</tr>
Am I incorrect with this path to the photo?
If it helps, I am running my web application from Tomcat which is in the directory
C:\Tomcat6
I will eventually be moving this over to a linux machine and expect to share the same path to the photo.
There is one major misconception here. HTML is executed by the webbrowser, not by the webserver. The webbrowser downloads HTML, scans for any resources which needs to be downloaded as well (CSS, scripts, images, etc) and fires a new HTTP request for each of them. All resources should point to a valid URL, not to some local disk file system path which the client machine has no notion of.
There are basically two ways to solve this "problem":
Add a new Context to Tomcat's /conf/server.xml:
<Context docBase="/usr/local/agent" path="/images" />
This way they'll be accessible through http://example.com/images/... and you'll be able to use the following <img>
<img src="/images/photo-name-here.jpg"/>
Create a Servlet which basically gets an InputStream of the image and writes it to the OutputStream of the response along a correct set of headers. You can find here a basic example of such a servlet and here a more advanced example. When the Servlet is mapped on /images/* in web.xml, the images are accessible by http://example.com/contextname/images/... and you'll be able to use it as follows (assuming that the JSP/HTML file is located in the context root):
<img src="images/photo-name-here.jpg"/>
src="/usr/local/agent/photo-name-here.jpg" <- this URL is a local address in your server, to show up your images you have to set a valid HTTP address like:
http://www.yourdomain.com/images/photo-name-here.jpg
To accomplish that you will need to upload the foto to a localpath that is inside in your www root folder.
If your webapp is installed in
/home/apache/www/website/
you will upload your images to a folder like:
/home/apache/www/website/images/
and then your HTTP address will be
http://www.yourdomain.com/images/photo-name-here.jpg
I got a little confuse with your two paths in /usr/ and C:\Tomcat
I encourage you to put the upload localpath folder parametrized, so you will be only modifying the config file instead of every function or method that access to that local path.