java上传文件在Tomcat下,怎么上传在项目指定目录??

如题所述

第1个回答  2012-06-25
你使用的那个上传组件啊?

struts2 默认提供的那个?
common-file-upload ?
要是使用的是struts 使用域驱动模式 在文件名上使用 *File 就行啊,然后你径读取就行了啊。追问

String path =ServletActionContext.getServletContext().getRealPath("/")+ this.getDestPath()+"\\"+ getFileFileName();
FileInputStream fin = new FileInputStream(file); FileOutputStream fout = new FileOutputStream(path);
byte [] buffer = new byte [BUFFER_SIZE];
while (fin.read(buffer) > 0 ) {
fout.write(buffer);
}
path还是Tomcat的路径

追答

确实是啊 你调用的方法不就是 调用 application的真实路径么 当然是 webapp/...... TomCat的项目路径啊。

你在输出流的时候写一个指定的 File 不就行了。然后对这个新建的 file 进行操作就可以了啊

File f= new File("c://newfile/my_file.txt");
然后输出流往这里写就行啊

第2个回答  推荐于2016-05-28
String path = request.getSession().getServletContext().getRealPath("/file");
OutputStream os = new FileOutputStream(path+"/"+Filename);
path是个字符串,你可以自己定义本回答被提问者和网友采纳
第3个回答  2012-06-25
配置文件.看你用什么库了....追问

String path =ServletActionContext.getServletContext().getRealPath("/")+ this.getDestPath()+"\\"+ getFileFileName();
FileInputStream fin = new FileInputStream(file); FileOutputStream fout = new FileOutputStream(path);
byte [] buffer = new byte [BUFFER_SIZE];
while (fin.read(buffer) > 0 ) {
fout.write(buffer);
}
path还是Tomcat的路径

相似回答