java - Upload a Word Document with JSP -
the program working on allows user browse files on server , download them. download wrote jsp upload content client. passed name of file , sends contents. code have bellow works great text files when try download , open word document understand binary file text, weird squares, , error message ms word. not sure why incorrectly reading binary data. appreciated.
<%@ page import="java.io.*" %> <% final file app_root = new file(com.mdi.core.config.processconfiguration.getinstance().getstringvalue("app_root")); file f = new file(request.getparameter("path")); file parent = f; boolean valid = false; while(!valid && (parent = parent.getparentfile()) != null) { valid = parent.equals(app_root); } if(!valid) { response.senderror(403, "you can not download file!"); return; } response.addheader("content-disposition","attachment; filename="+f.getname()); response.setcontentlength((int)f.length()); outputstream os = response.getoutputstream(); response.setcontenttype(java.net.urlconnection.guesscontenttypefromname(f.getname())); inputstream fin = null; try { fin = new fileinputstream(f); byte[] buff = new byte[512]; int read; while((read = fin.read(buff)) != -1) os.write(buff, 0, read); } catch(exception ex) { ex.printstacktrace(); } { if(fin != null) fin.close(); } %>
also error in standard output.
java.net.protocolexception: exceeded stated content-length of: '40220' bytes @ weblogic.servlet.internal.servletoutputstreamimpl.checkcl(servletoutputstreamimpl.java:200) @ weblogic.servlet.internal.servletoutputstreamimpl.write(servletoutputstreamimpl.java:146) @ jsp_servlet._mdi._html._file_upload.__default_file_upload._jspservice(__default_file_upload.java:101) @ weblogic.servlet.jsp.jspbase.service(jspbase.java:34) @ weblogic.servlet.internal.stubsecurityhelper$servletserviceaction.run(stubsecurityhelper.java:227) @ weblogic.servlet.internal.stubsecurityhelper.invokeservlet(stubsecurityhelper.java:125) @ weblogic.servlet.internal.servletstubimpl.execute(servletstubimpl.java:300) @ weblogic.servlet.internal.tailfilter.dofilter(tailfilter.java:26) @ weblogic.servlet.internal.filterchainimpl.dofilter(filterchainimpl.java:56) @ com.mdi.core.utils.http.securityfilter.dofilter(securityfilter.java:154) @ weblogic.servlet.internal.filterchainimpl.dofilter(filterchainimpl.java:56) @ com.mdi.core.utils.http.responseheaderfilter.dofilter(responseheaderfilter.java:69) @ weblogic.servlet.internal.filterchainimpl.dofilter(filterchainimpl.java:56) @ weblogic.servlet.internal.webappservletcontext$servletinvocationaction.wraprun(webappservletcontext.java:3715) @ weblogic.servlet.internal.webappservletcontext$servletinvocationaction.run(webappservletcontext.java:3681) @ weblogic.security.acl.internal.authenticatedsubject.doas(authenticatedsubject.java:321) @ weblogic.security.service.securitymanager.runas(securitymanager.java:120) @ weblogic.servlet.internal.webappservletcontext.securedexecute(webappservletcontext.java:2277) @ weblogic.servlet.internal.webappservletcontext.execute(webappservletcontext.java:2183) @ weblogic.servlet.internal.servletrequestimpl.run(servletrequestimpl.java:1454) @ weblogic.work.executethread.execute(executethread.java:209) @ weblogic.work.executethread.run(executethread.java:178) <jun 27, 2014 10:46:07 edt> <error> <http> <bea-101083> <connection failure. java.net.protocolexception: didn't meet stated content-length, wrote: '39942' bytes instead of stated: '40220' bytes. @ weblogic.servlet.internal.servletoutputstreamimpl.ensurecontentlength(servletoutputstreamimpl.java:446) @ weblogic.servlet.internal.servletresponseimpl.ensurecontentlength(servletresponseimpl.java:1432) @ weblogic.servlet.internal.servletresponseimpl.send(servletresponseimpl.java:1511) @ weblogic.servlet.internal.servletrequestimpl.run(servletrequestimpl.java:1462) @ weblogic.work.executethread.execute(executethread.java:209) truncated. see log file complete stacktrace > <jun 27, 2014 10:46:07 edt> <error> <http> <bea-101104> <servlet execution in servlet context "servletcontext@1188950474[app:trade module:ta15 path:/ta15 spec-version:null]" failed, java.net.protocolexception: didn't meet stated content-length, wrote: '39942' bytes instead of stated: '40220' bytes.. java.net.protocolexception: didn't meet stated content-length, wrote: '39942' bytes instead of stated: '40220' bytes. @ weblogic.servlet.internal.servletoutputstreamimpl.ensurecontentlength(servletoutputstreamimpl.java:446) @ weblogic.servlet.internal.servletresponseimpl.ensurecontentlength(servletresponseimpl.java:1432) @ weblogic.servlet.internal.servletresponseimpl.send(servletresponseimpl.java:1511) @ weblogic.servlet.internal.servletrequestimpl.run(servletrequestimpl.java:1462) @ weblogic.work.executethread.execute(executethread.java:209) truncated. see log file complete stacktrace
very important remove jsp own whitespace output:
<%@ page contenttype="application/octet" import="java.io.*" %><% %>
no white space, , no newline on last line. tip: use servlet.
somehow os.flush()
still needs done.
after comment @boristhespide: forget flushing. think spaces caused problem, added content type binary data.
Comments
Post a Comment