JSP如何取得<input type="file"> 文件路径

 我来答
倪采雁
2013-08-24 · TA获得超过968个赞
知道答主
回答量:132
采纳率:85%
帮助的人:74.5万
展开全部
问题补充:我使用strFile = request.getParameter("file")只能取得文件名,会发生I/O异常,提示找不到文件路径; <br />用new File(strFile).getAbsolutePath()得到了路径,但是得到的路径形式为:%Tomcat_dir%\bin\XX,XX为文件名 问题补充:<div class="quote_title">lyyf 写道</div><div class="quote_div">1.用request.getParameter(String str) <br /><input type="file" name="fileName" <br />后台用String fileName=request.getParameter("fileName")获取,试试 <br />2.直接获取服务器上的内容 <br /> FileItemFactory factory = new DiskFileItemFactory(); <br /> ServletFileUpload upload = new ServletFileUpload(factory); <br /> List<FileItem list = upload.parseRequest(request); <br />不知道对不对,希望对你有帮助 <br /> <br /> <br /> <br /></div> <br />String fileName=request.getParameter("fileName")这个我试过了只能得到文件名,浏览器我用IE9、firefox 4试过都不行;我仅仅是想获得<input type="file" name="fileName"的路径 问题补充:<div class="quote_title">maxm 写道</div><div class="quote_div">1.是以post方式提交的的吗 <br />2.有无 :enctype= "multipart/form-data " <br />3.可以用隐藏iframe的方式</div> <br /> <br /> <br />是post提交,如果有enctype= "multipart/form-data "的话,程序就出错,运行起来没反应,我在IE6试了下,可以取得路径(不过此时我的程序能正常运行,但是有乱码出现) 问题补充:<div class="quote_title">maxm 写道</div><div class="quote_div">贴出异常信息</div> <br /> <br />Path is:F:\apache-tomcat-6.0.29\bin\log.txt(这是我使用new File(strFile).getAbsolutePath();取得的路径) <br />javax.mail.MessagingException: IOException while sending message; <br /> nested exception is: <br /> java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />Caused by: java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> ... 20 more <br />java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。) <br /> at java.io.FileInputStream.open(Native Method) <br /> at java.io.FileInputStream.<init(Unknown Source) <br /> at javax.activation.FileDataSource.getInputStream(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852) <br /> at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453) <br /> at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98) <br /> at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) <br /> at javax.activation.DataHandler.writeTo(Unknown Source) <br /> at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383) <br /> at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743) <br /> at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888) <br /> at service.Mail.startSend(Mail.java:460) <br /> at org.apache.jsp.send_jsp._jspService(send_jsp.java:178) <br /> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377) <br /> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) <br /> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) <br /> at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) <br /> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) <br /> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) <br /> at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) <br /> at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) <br /> at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) <br /> at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) <br /> at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) <br /> at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) <br /> at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861) <br /> at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579) <br /> at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584) <br /> at java.lang.Thread.run(Unknown Source) <br />
小傻

2015-10-27 · 知道合伙人软件行家
小傻
知道合伙人软件行家
采纳数:11567 获赞数:31134
已经做过两个上架的app和两个网页项目.

向TA提问 私信TA
展开全部
JSP取得<input type="file"> 文件路径方式如下:
1.用request.getParameter(String str)
<input type="file" name="fileName">
后台用String fileName=request.getParameter("fileName")获取,试试
2.直接获取服务器上的内容
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> list = upload.parseRequest(request);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式