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

2024年11月16日 13:00
有2个网友回答
网友(1):

问题补轿衫轮充:我使用strFile = request.getParameter("file"塌誉)只能取得文件名,会发生I/O异常,提示找不到文件路径;
用new File(strFile).getAbsolutePath()得到了路径,但是得到的路径形式为:%Tomcat_dir%\bin\XX,XX为文件名 问题补充:

lyyf 写道
1.用request.getParameter(String str)
后台用String fileName=request.getParameter("fileName")获取,试试
2.直接获取服务器上的内容
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List不知道对不对,希望对你有帮助




String fileName=request.getParameter("fileName")这个我试过了只能得到文件名,浏览器我用IE9、firefox 4试过都不行;我仅仅是想获得maxm 写道
1.是以post方式提交的的吗
2.有无 :enctype= "multipart/form-data "
3.可以用隐藏iframe的方式


闭信
是post提交,如果有enctype= "multipart/form-data "的话,程序就出错,运行起来没反应,我在IE6试了下,可以取得路径(不过此时我的程序能正常运行,但是有乱码出现) 问题补充:
maxm 写道
贴出异常信息


Path is:F:\apache-tomcat-6.0.29\bin\log.txt(这是我使用new File(strFile).getAbsolutePath();取得的路径)
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930)
at service.Mail.startSend(Mail.java:460)
at org.apache.jsp.send_jsp._jspService(send_jsp.java:178)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream. at javax.activation.FileDataSource.getInputStream(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888)
... 20 more
java.io.FileNotFoundException: F:\apache-tomcat-6.0.29\bin\log.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream. at javax.activation.FileDataSource.getInputStream(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:852)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:453)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:98)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888)
at service.Mail.startSend(Mail.java:460)
at org.apache.jsp.send_jsp._jspService(send_jsp.java:178)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
at java.lang.Thread.run(Unknown Source)

网友(2):

JSP取得 文件路径方式梁纤培如下:
1.用request.getParameter(String str)

后台用String fileName=request.getParameter("fileName")获取,试试
2.直接获取服务器上的内容
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List list = upload.parseRequest(request);

相关问答