java - Image does not load in external WebBrowser (Firefox) -
i have created dynamic webproject , using following code upload image , retrieve same location.now when trying run application in external browser shows rectangle box instead of image.getting loaded in external web browser gives error.
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.iterator; import java.util.list; import java.util.random; import java.util.regex.matcher; import java.util.regex.pattern; import java.sql.*; import org.apache.commons.fileupload.servlet.servletfileupload; import org.apache.commons.fileupload.disk.*; import org.apache.commons.fileupload.*; public class uploadimage extends httpservlet { private static final long serialversionuid = 1l; public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); boolean ismultipart = servletfileupload.ismultipartcontent(request); system.out.println("request: " + request); if (!ismultipart) { system.out.println("file not uploaded"); } else { fileitemfactory factory = new diskfileitemfactory(); servletfileupload upload = new servletfileupload(factory); list items = null; try { items = upload.parserequest(request); system.out.println("items: " + items); } catch (fileuploadexception e) { e.printstacktrace(); } iterator itr = items.iterator(); while (itr.hasnext()) { fileitem item = (fileitem) itr.next(); if (item.isformfield()) { string name = item.getfieldname(); system.out.println("name: " + name); string value = item.getstring(); system.out.println("value: " + value); } else { try { string itemname = item.getname(); random generator = new random(); int r = math.abs(generator.nextint()); string reg = "[.*]"; string replacingtext = ""; system.out.println("text before replacing is:-"+ itemname); pattern pattern = pattern.compile(reg); matcher matcher = pattern.matcher(itemname); stringbuffer buffer = new stringbuffer(); while (matcher.find()) { matcher.appendreplacement(buffer, replacingtext); } int indexof = itemname.indexof("."); string domainname = itemname.substring(indexof); system.out.println("domainname: " + domainname); string finalimage = buffer.tostring() + "_" + r+ domainname; system.out.println("final image===" + finalimage); file savedfile = new file("d:\\test1\\" + "images\\"+ finalimage); item.write(savedfile); out.println("<html>"); out.println("<body>"); out.println("<table><tr><td>"); out.println("<img src=d:\\test1\\images\\" + finalimage+ ">"); out.println("</td></tr></table>"); connection conn = null; string url = "jdbc:oracle:thin:@//localhost:1521/erp"; string username = "system"; string userpassword = "manager"; string strquery = null; try { system.out.println("itemname::::: " + itemname); class.forname("oracle.jdbc.driver.oracledriver"); conn = drivermanager.getconnection(url, username,userpassword); statement st = conn.createstatement(); strquery = "insert testimage values('"+ finalimage + "')"; int rs = st.executeupdate(strquery); system.out.println("query executed successfully++++++++++++++"); out.println("image inserted successfully"); out.println("</body>"); out.println("</html>"); } catch (exception e) { system.out.println(e.getmessage()); } { conn.close(); } } catch (exception e) { e.printstacktrace(); } } } } } }
the process follows:
- enduser requests web page on url.
- webbrowser sends http request webserver on given url.
- webserver retrieves http request , returns desired web page http response.
- webbrowser retrieves http response in flavor of html code.
- webbrowser parses html code in order present , encounters
<img>
element. - webbrowser attempts download image location specified in
src
attribute.
and there fails. specified fixed local disk file system path valid if webbrowser runs @ physically same machine webserver (as occur in development environment, absolutely not in production environment). can't , shouldn't expect enduser has desired image @ given location of local disk file system. more, can't , shouldn't expect enduser has d:
disk, let alone enduser running windows.
you should instead specifying fullworthy url in image's src
attribute. 1 enter in webbrowser's address bar in order see image, starting http://
or https://
scheme.
there several ways achieve this, 2 popular ways are:
expose upload folder virtual host. done in server config. didn't tell 1 you're using, let's assume it's tomcat many starters use: open
/conf/server.xml
file , add following element<host>
element:<context docbase="d:\test1\images" path="/images" />
the
docbase
specifies root folder files reside ,path
specifies context path see after domain part in url. way images accessible throughhttp://example.com/images/...
.out.println("<img src=\"/images/" + finalimage+ "\">");
create simple servlet reads image disk , writes response after having set desired response headers telling browser file content type, length , disposition browser knows how deal it:
@webservlet("/images/*") public class imageservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string filename = urldecoder.decode(request.getpathinfo(), "utf-8"); file file = new file("d:\\test1\\images", filename); response.setheader("content-type", getservletcontext().getmimetype(file.getname())); response.setheader("content-length", string.valueof(file.length())); response.setheader("content-disposition", "inline; filename=\"" + file.getname() + "\""); files.copy(file.topath(), response.getoutputstream()); } }
also way images accessible through
http://example.com/images/...
.out.println("<img src=\"/images/" + finalimage+ "\">");
unrelated concrete problem, emitting html in servlet poor practice. should use jsp that. see coding style , recommendations section of our servlets wiki page.
have further thought uploaded images happen have same file name existing uploaded image?
Comments
Post a Comment