使用Jsp上传图像时出现问题 [英] Problem with uploading images using Jsp
问题描述
我的要求是通过jsp将图像上传到数据库。为此,我编写了一个代码。它工作正常。但是文件imgfile =新文件(C:/ Program Files / Apache Software Foundation / Tomcat 5.5 / webapps / test / Blue.jpg);而不是我使用File imgfile = new File(" Blue.jpg");就是这样它没有用。当我们从本地系统浏览该图像时,它会像Blue.jpg一样自动显示并且没有完整路径。
所以我得到的错误是找不到Blue.jpg。请告诉解决方案我该如何解决我的问题。
< blockquote>这不是使用JSP附加图像的方式。相反,您应该在JSP上有一个表单,其输入类型为file,这样用户就可以选择该文件并上传它。然后,您需要使用Apache的commons-fileupload.jar等服务器在服务器上读取该表单的请求。
PS不要连接到JSP代码中的数据库。在另一个班级做这件事。 JSP用于表示。
如果您想使用servlet而不是jsp文件来上传文件就是一个例子。
如何使用Java Servlet将文件插入MySQL blob
@ ak1dnar
此源代码似乎将文件保存到磁盘(上传的文件),然后从中读取此文件磁盘,然后将其保存到数据库。
但是,如果要将其保存在mySql数据库中,为什么要先将它保存在磁盘上(每个人都可以访问它,例如扫描的护照)?
在
主题中查看我的解决方案标题为图像上传和从BLOB中检索jsp
如何将其直接保存到数据库。 (不先将其保存到磁盘上)
Hi,
My Requirement is to upload the images into database through jsp. For that i write a code .And it works fine .But in that File imgfile = new File("C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/test/Blue.jpg"); instead of that i used File imgfile = new File("Blue.jpg"); like that it didn''t work. When we browse that image from local system it automatically takes like Blue.jpg and didn''t take the full path.
So i got the error as " Blue.jpg not found". Please tell the solution how can i resolve my problem.
That is not how you attach images using JSPs. Instead you should have a form on your JSP with an input of type file so users can pick the file and upload it. You then need to read that form''s request on the server using something like Apache''s commons-fileupload.jar.
P.S Don''t connect to databases in JSP code. Do that in a separate class. JSPs are for presentation.
If you like to use a servlet instead of a jsp file for uploading your files here is an example.
How to Insert files to MySQL blob using Java Servlet
@ak1dnar
This sourcecode seems to save a file to disk (the uploaded file), then read this file from disk and then save it to database.
But why save it on disk first (where everybody can access it, for example a scanned passport) if you want to save it inside the mySql database?
See my solution in the
thread titled "Image upload and retrieve from BLOB in jsp"
how to save it directly into the database. (without saving it to disk first)
这篇关于使用Jsp上传图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!