请问javaweb高手,有个关于jsp上传文件的问题很难解决,请问高手解答?

在servlet中,我想得到jsp表单上传的文件的硬盘的绝对路径,但是由于浏览器的安全性原因,只能得到文件名,不能得到全路径,使我无法用new file(filepath)创建文件。
如jsp中:
<form action="servlet/ManageAlbum?sub=UPLOAD" method="post">
<input type="file" name="filepath" id="filepath" />
<input type="submit" value="upload"/>
</form>
在servlet中用filepath=request.getParameter("filepath");报错,因为filepath只是文件名,不是路径名。
请问jsp该如何向servlet中传一个文件输入流?

第1个回答  2013-02-18
不是像servlet中传入文件输入流,而是servlet获得文件对象,然后在servlet里面写文件输入流。你能获得文件名就应该获得了文件对象。定义一个文件路径,然后用流的方式写文件,这就是文件上传。
第2个回答  2013-02-18
这个是需要通过其他的变量来获得到的。
ServletContext servletContext = ServletActionContext.getServletContext();
String dataDir = servletContext.getRealPath("/file/upload");

请参考:
http://zhidao.baidu.com/question/277345613.html

那你试着用request.getSession().getServletContext().getRealPath("/file/upload")其实只是实现体的区别,只要实现了都可以的。你用的哪个就用哪个的实现。本回答被提问者采纳
第3个回答  2013-02-18
//获得路径
String targetpath = ServletActionContext.getServletContext()
.getRealPath("/"+ UploadConfigurationRead.getInstance().getConfigItem("uploadPath").trim())
第4个回答  2013-02-18
最好使用第三方架包。使用字节流传输
相似回答