文件上传问题

不夠勇敢 2013-11-12 06:19:04
为什么我上传什么都到tomcat下发布的路径下去了 。而工程项目下什么都没有。这是什么情况。代码没问题?????????????????????求解
...全文
534 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
观鱼塘主 2013-11-14
  • 打赏
  • 举报
回复
tomcat就是你的服务器,所以你上传的文件会在tomcat下,这是毋庸置疑的。 如果你想上传到别的地方,那么在后台写入文件的时候你就要写明绝对路径 下面是Demo项目的后台代码:
package servlet;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadifyDemo extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		uploadify(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		doGet(req, resp);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void uploadify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		String dir = "upload/" + sdf.format(new Date());//按日期生成存放目录
		String savePath = this.getServletConfig().getServletContext()
				.getRealPath(dir);
		File f1 = new File(savePath);
		if (!f1.exists()) {
			f1.mkdirs();
		}
		f1 = null;

		DiskFileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("UTF-8");
		List fileList = null;
		try {
			fileList = upload.parseRequest(req);
		} catch (FileUploadException ex) {
			ex.printStackTrace();
			return;
		}
		if(fileList == null){
			return;
		}
		Iterator<FileItem> it = fileList.iterator();
		String name = "";
		String extName = "";
		while (it.hasNext()) {
			FileItem item = it.next();
			if (!item.isFormField()) {
				name = item.getName();
				if (name == null || name.trim().equals("")) {
					continue;
				}

				//文件后缀名 如:.jpg
				if (name.lastIndexOf(".") >= 0) {
					extName = name.substring(name.lastIndexOf("."));
				}
				//\s去除任何空空白符如:  空格符、制表符和进纸符等
				String fileName = name.replaceAll("\\s", "");
				
				fileName = fileName.substring(0, fileName.lastIndexOf(".") - 1);
				
				// 仅仅是为了判断当前命名的文件是否已存在
				File f = new File(savePath + "/" + fileName + extName);
				
				while (f.exists()) {//如果文件存在,在文件名后加上随机数做区分

					/* 日期+100以内的随机数 */
					int rand = (int) Math.round(Math.random() * 100);
					sdf = new SimpleDateFormat("HHMMss");
					
					fileName += "_"+sdf.format(new Date()) + rand;
					f = new File(savePath + "/" + fileName + extName);
				}
				f = null;

				File saveFile = new File(savePath + "/" + fileName
						+ extName);
				try {
					item.write(saveFile);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println(dir + "/" + fileName + extName);//返回相对根路径:http://xx.xx.com/upload/songs/2010/06/05/ring.mp3
				
				resp.getWriter().print("文件" + fileName + "上传成功");
			}
		}
	}
}
ottorz 2013-11-13
  • 打赏
  • 举报
回复
看看你设置路径的代码
zlr1004 2013-11-13
  • 打赏
  • 举报
回复
看看你的源码啊
xiaomm627 2013-11-13
  • 打赏
  • 举报
回复
你说的工程是开发环境的路径吗? 那上传肯定是上传到运行环境的路径(tomcat)下啊。 你所谓的工程是开发环境。
咔哇伊 2013-11-13
  • 打赏
  • 举报
回复
需要源码,不然怎么看
别闹腰不好 2013-11-13
  • 打赏
  • 举报
回复
引用 5 楼 u012463264 的回复:
哎 上传 不传到服务器上,会传到本机吗
你写的路径有问题,如果你写的是相对路径,会传到tomcat的bin文件夹下。
别闹腰不好 2013-11-13
  • 打赏
  • 举报
回复
哎 上传 不传到服务器上,会传到本机吗
Defonds 2013-11-13
  • 打赏
  • 举报
回复
lixingxu828 2013-11-13
  • 打赏
  • 举报
回复
tomcat是你的运行环境,当然是在你的运行环境下面的噻,当然不会在你的开发环境下面的哈,eclips下面的仅仅是开发环境,不作运行时用,所以不是上传的地址。
jssqchenjin 2013-11-13
  • 打赏
  • 举报
回复
是你设置的路径有问题呗。打断点看下哪里的问题
kobe8free 2013-11-12
  • 打赏
  • 举报
回复
你的工程不就是在tomcat下吗!如果你需要在工程下 你就在 getrealpath()+"\\path"
不夠勇敢 2013-11-12
  • 打赏
  • 举报
回复
引用 1 楼 stubble 的回复:
上传路径的问题吧
说了每次获得的都是tomcat下的路径, 什么方法都不行我试了很久了
异常异长 2013-11-12
  • 打赏
  • 举报
回复
上传路径的问题吧

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧