日期:2014-05-20  浏览次数:20638 次

fileUpload上传问题
[code=Java][/code]

这样上传后 上传的文件怎么不保存到项目的Webroot目录的upload下 ,反而保存到Tomcat里的upload了 , 这是怎么回事。


DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload");
System.out.println(path);

factory.setRepository(new File(path));
factory.setSizeThreshold(1024 * 1024);

ServletFileUpload upload = new ServletFileUpload(factory);

try {
List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
for(FileItem item : list){
String name = item.getFieldName();
if(item.isFormField())
{

String value = item.getString();
System.out.println(name + "=" + value);
request.setAttribute(name,value);
}else
{
String value = item.getName();
System.out.println(value);

int start = value.lastIndexOf("\\");
String fileName = value.substring(start + 1);

request.setAttribute(name,fileName);
item.write(new File(path,fileName));
}
}
} catch (Exception e) {
// TODO: handle exception
}
request.getRequestDispatcher("UploadResult.jsp").forward(request,response);


------解决方案--------------------
估计你使用的是部署 你部署之后实际上tomcat是在webapp里面运行项目 你自己配置下context指向你当前项目就行了
http://tomcat.apache.org/tomcat-4.1-doc/config/context.html
------解决方案--------------------
你的应用程序是跑在tomcat里面的String path = request.getRealPath("/upload");得到的路径就是webApp,不在这在哪里啊
------解决方案--------------------
楼主参考一下我的代码:
HTML code

<form action="upload" method="post" enctype="multipart/form-data">
文件名称:<input type="file" name="file"/>
<br />存储到服务器上的路径为:<input type="text"name="path" /><br />
<INPUT TYPE="submit" value="上传">
<INPUT TYPE="reset" value="清除">
</form>

------解决方案--------------------
就应该在tomcat下的