J2EE中用户上传文件到服务器功能实现???

小灶 发布于 2013/01/22 14:16
阅读 4K+
收藏 0

@jeffsui 你好,想跟你请教个问题:

大婶(错了应该是大神。。。)

我现在把网站的下载功能做好了;不过下面做的是让用户把写好的文档传到服务器上,我们这边可以下载到本地。。。应该是用叫什么getpath();

具体框架代码怎么写啊?

求大婶指点迷津!!!回头咱们加好友一起研究各自困惑的问题

加载中
0
JavaOlder
JavaOlder

引用来自“JavaNewPer”的答案

附一段 commons-fileupload上传组件的servlet实现代码

public class FileUploadServlet extends AbstractItemServlet {
		
	private File uploadPath;
	
	private File tempPath;
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		//form提交采用multipart/form-data,无法采用req.getParameter()取得数据
		//String itemNo = req.getParameter("itemNo");
		//System.out.println("itemNo======" + itemNo);
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// maximum size that will be stored in memory
		factory.setSizeThreshold(4096);
		// the location for saving data that is larger than getSizeThreshold()
		factory.setRepository(tempPath);

		ServletFileUpload upload = new ServletFileUpload(factory);
		// maximum size before a FileUploadException will be thrown
		upload.setSizeMax(1000000 * 20);
		try {
			List fileItems = upload.parseRequest(req);
			String itemNo = "";
			for (Iterator iter = fileItems.iterator(); iter.hasNext();) {
				FileItem item = (FileItem) iter.next();
				
				//是普通的表单输入域
				if(item.isFormField()) {
					if ("itemNo".equals(item.getFieldName())) {
						itemNo = item.getString();
					}
				}
				//是否为input="type"输入域
				if (!item.isFormField()) {
					String fileName = item.getName();
					long size = item.getSize();
					if ((fileName == null || fileName.equals("")) && size == 0) {
						continue;
					}
					//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
					fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
					item.write(new File(uploadPath, fileName));
					itemManager.uploadItemImage(itemNo, fileName);
				}
			}
			res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException("上传失败!");
		}
	}

	public void init() throws ServletException {
		uploadPath = new File(getServletContext().getRealPath("upload"));
		System.out.println("uploadPath=====" + uploadPath);
		//如果目录不存在
		if (!uploadPath.exists()) {
			//创建目录
			uploadPath.mkdir();
		}
		
		//临时目录
		tempPath = new File(getServletContext().getRealPath("temp"));
		if (!tempPath.exists()) {
			tempPath.mkdir();
		}
		//显示调用父类的init方法
		super.init();
	}
}

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>index.html</title>

		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="this is my page">

		<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

	</head>

	<body>
		<form action="./servlet/item/FileUploadServlet" method="post"
			enctype="multipart/form-data" name="form1">
			<input type="file" name="file">
			<input type="submit" name="Submit" value="upload">
		</form>
		<form action="./servlet/HelloWord" method="post">
			<input type="submit" />
		</form>
		<form name="uploadform" method="POST" action="./servlet/item/FileUploadServlet"
			ENCTYPE="multipart/form-data">

			<table border="1" width="450" cellpadding="4" cellspacing="2"
				bordercolor="#9BD7FF">

				<tr>
					<td width="100%" colspan="2">

						文件1:
						<input name="x" size="40" type="file">

					</td>
				</tr>

				<tr>
					<td width="100%" colspan="2">

						文件2:
						<input name="y" size="40" type="file">

					</td>
				</tr>

				<tr>
					<td width="100%" colspan="2">

						文件3:
						<input name="z" size="40" type="file">

					</td>
				</tr>

			</table>

			<br />
			<br />

			<table>

				<tr>
					<td align="center">
						<input name="upload" type="submit" value="开始上传" />
					</td>
				</tr>

			</table>

		</form>

	</body>
</html>
小灶
小灶
回复 @JavaNewPer : 谢谢大哥
JavaOlder
JavaOlder
回复 @小灶 : 源码里面不是有路径么?路径你自己选择吧。你运行一下代码就晓得了!
小灶
小灶
大哥问一下我改了一下JSP页面的代码,基本的模块出来了,请问一下文件上传到服务器放在哪啊?然后我们主服务器这可以下载
0
恺哥
小灶
小灶
大哥谢谢。我好好看看,有什么问题咱们再沟通,,,
0
jeffsui
jeffsui

首先我不是大神,我只是个普通搞It的。

文件上传有很多实现。

1.commons-fileUpload框架

2.cos 开源中国用的就是这个组件

3.http

当然还有其他方式,你可以自己选择!

小灶
小灶
请问您有差不多的框架代码吗?小弟参考一下,真的没思路啊大哥
0
JavaOlder
JavaOlder

附一段 commons-fileupload上传组件的servlet实现代码

public class FileUploadServlet extends AbstractItemServlet {
		
	private File uploadPath;
	
	private File tempPath;
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		//form提交采用multipart/form-data,无法采用req.getParameter()取得数据
		//String itemNo = req.getParameter("itemNo");
		//System.out.println("itemNo======" + itemNo);
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// maximum size that will be stored in memory
		factory.setSizeThreshold(4096);
		// the location for saving data that is larger than getSizeThreshold()
		factory.setRepository(tempPath);

		ServletFileUpload upload = new ServletFileUpload(factory);
		// maximum size before a FileUploadException will be thrown
		upload.setSizeMax(1000000 * 20);
		try {
			List fileItems = upload.parseRequest(req);
			String itemNo = "";
			for (Iterator iter = fileItems.iterator(); iter.hasNext();) {
				FileItem item = (FileItem) iter.next();
				
				//是普通的表单输入域
				if(item.isFormField()) {
					if ("itemNo".equals(item.getFieldName())) {
						itemNo = item.getString();
					}
				}
				//是否为input="type"输入域
				if (!item.isFormField()) {
					String fileName = item.getName();
					long size = item.getSize();
					if ((fileName == null || fileName.equals("")) && size == 0) {
						continue;
					}
					//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
					fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
					item.write(new File(uploadPath, fileName));
					itemManager.uploadItemImage(itemNo, fileName);
				}
			}
			res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ApplicationException("上传失败!");
		}
	}

	public void init() throws ServletException {
		uploadPath = new File(getServletContext().getRealPath("upload"));
		System.out.println("uploadPath=====" + uploadPath);
		//如果目录不存在
		if (!uploadPath.exists()) {
			//创建目录
			uploadPath.mkdir();
		}
		
		//临时目录
		tempPath = new File(getServletContext().getRealPath("temp"));
		if (!tempPath.exists()) {
			tempPath.mkdir();
		}
		//显示调用父类的init方法
		super.init();
	}
}

 

小灶
小灶
回复 @JavaNewPer : 老大,有没有在JSP页面上直接实现的代码啊???
JavaOlder
JavaOlder
回复 @小灶 : 最佳答案喽~
小灶
小灶
谢谢大哥!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
恺哥
恺哥
osc上的资源很多的,要善于利用,呵呵
小灶
小灶
谢谢,我还是个小小的菜鸟以后咱们常沟通。。。
0
恺哥
恺哥
大家都是从小小鸟过来的
小灶
小灶
呵呵就是
返回顶部
顶部