简体   繁体   English

如何在数据库中保存图像路径

[英]How to save Image path in database

I am working on web application in which user can upload a image say at(D:\\media). 我正在使用Web应用程序,其中用户可以在(D:\\ media)上传图像。 I have added in host param of my server.xml of tomcat 我在tomcat的server.xml中添加了主机参数

<Context docBase="D:\media" path="/media" />

So now every Image at"D:\\media" can be viewed at "localhost:8080/media" as(take example of abc.png) 因此,现在可以在“ localhost:8080 / media”中以“ D:\\ media”的每个图像查看(例如abc.png)

localhost:8080/media/abc.png

I am just showing image in jsp when required as 我只是在需要时在jsp中显示图像

<img src="localhost:8080/media/abc.png">

my question is how and which imagepath to store in database when user uploads image 我的问题是当用户上传图像时如何以及在数据库中存储哪个imagepath

You can save the image right to the database from the servlet that process the post request. 您可以从处理发布请求的servlet中将图像权限保存到数据库中。 You must use this attribute in the form: enctype='multipart/form-data' and then get the Part with its parameter name like this: 您必须以以下形式使用此属性:enctype ='multipart / form-data',然后使用其参数名称获取Part,如下所示:

//request in the HttpServletRequest.
Part uploadedFile = request.getPart("parameterName");
InputStream is = uploadedFile.getInputStream();
byte[] fileData = new byte[add a preferred size];
is.read(fileData);
//Save fileData in the database.

Here you can have more info with examples: http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part 在这里,您可以获取有关示例的更多信息: http : //www.programcreek.com/java-api-examples/index.php? api= javax.servlet.http.Part

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM