大家有没有做过用java上传文件夹的(注意:是上传文件夹,不是文件)

Sky__ 发布于 2010/10/19 21:25
阅读 6K+
收藏 3

大家有没有做过用java上传文件夹的(注意:是上传文件夹,不是文件)

有的话,给点代码提示提示啊.

加载中
0
Jeecn
Jeecn

这个问题要这么解决:

循环遍历文件夹,把所有的目录名字和级别放到数组或list、map等容器里边

在服务器上创建目录,然后用流读取文件,ok!

0
红薯
红薯

这个问题跟java没关系啊,关键是浏览器不支持啊

wellyao
wellyao
我觉得这个应该是正解....
0
jumkey
jumkey
可以看看html5 js2的api他支持文件操作,我做过多文件拖拽ajax上传
0
JavaGG
JavaGG

用javascript试试吧

0
雪人
雪人

applet可以

0
Jeecn
Jeecn

引用来自#3楼“红薯”的帖子

这个问题跟java没关系啊,关键是浏览器不支持啊

但是可以用另一种方法达到目的,自己创建不就可以了吗

文件总可以上传的哦

0
Sky__
Sky__

引用来自#4楼“Jumkey”的帖子

可以看看html5 js2的api他支持文件操作,我做过多文件拖拽ajax上传

 可不可以给个范例给我呀?html5目前不是很大一部分浏览器不支持么?

我觉得applet才是正解, 因为,j2ee服务器是不能操作到客户端的

0
Sky__
Sky__

引用来自#3楼“红薯”的帖子

这个问题跟java没关系啊,关键是浏览器不支持啊

 applet 和 active x  但是activex 不会

0
jumkey
jumkey

引用来自#8楼“袁柯”的帖子

引用来自#4楼“Jumkey”的帖子

可以看看html5 js2的api他支持文件操作,我做过多文件拖拽ajax上传

 可不可以给个范例给我呀?html5目前不是很大一部分浏览器不支持么?

我觉得applet才是正解, 因为,j2ee服务器是不能操作到客户端的

<!DOCTYPE html>
<html>
    <head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<title>托拽上传文件</title>
		<style type="text/css">
			.data_content {
			  	border: 4px solid green;
				padding: 10px;
				margin-top: 10px;
			}
			#way {
				padding: 10px;
				border-bottom: 2px solid #ddd;
			}
			#way:hover {
			}
			.dropzone {
				border: 4px dashed #aaa;
				height: 180px;
				width: 400px;
			}
			.dropzone:hover {
				border-color: #777;
			}
			#dropable_file {
				font-size: 100px;
				opacity: 0;	
				padding: 30px;
				height: 100%;
				width: auto;
				position: relative;
			}
			.drop_file {
				overflow:hidden;
				background:url(download.png) center bottom no-repeat;
				border: 4px dashed #aaa;
			}
			.drop_file:hover {
				background-image: url(download_on.png);
			}
			input:focus {
				outline:none;	
			}
			#message .loader {
				display:inline-block;
				position:relative;
				top:3px;
				width:150px;
				height:12px;
				margin-left:20px;
				border-radius:5px;
				border:1px solid rgb(79, 99, 99);
			}
			#message .loader div {
				height:12px;
				width:0px;
				background-color:gray;
			}
		</style>
		<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
		<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
        <script type="text/javascript">
			var FileManager={
				//上传文件数0开始
				fileCount:-1,
				// 通过Ajax将File对象发送到服务器端
				uploadFiles:function(files) {
					$("#message").append("上传" + files.length + "个文件...<br />");
					for(var i=0; i < files.length; i++) {
						$("#message").append(files[i].fileName+"<br/><div class=\"loader\"><div></div></div><br/>");
						var xhr = new XMLHttpRequest;
						xhr.open('post', 'servlet/Upload?test=11111111111', true);
						
						this.fileCount=this.fileCount+1;
						xhr.upload.i1i=this.fileCount;//传递参数用
						
						xhr.upload.onprogress = function(e){
							console.log("showProgress");
							$(".loader div:eq("+this.i1i+")").css({width: Math.round(e.loaded/e.total*100)+"%"});
						};
						xhr.upload.onload = function(){
							console.log("finishLoad");
							$(".loader div:eq("+this.i1i+")").css({"background-color": "green"});
						};
						xhr.onprogress = function(){
							console.log("showAjaxProgress");
						};
						xhr.onload = function(){
							console.log("finishAjaxLoad");
						};
						xhr.onreadystatechange = function(){
							console.log("ready"+this.readyState);
							if (this.readyState != 4){
								return;
							}
							$("#message").append("<div class=\"data_content\">成功上传文件,文件内容:<hr /><pre>" + this.responseText + "</pre></div>");
						};
						
						xhr.setRequestHeader('Content-Type', 'multipart/form-data');
						xhr.setRequestHeader('X-File-Name', encodeURI(files[i].fileName)); // 将文件名传递给服务器端
						xhr.setRequestHeader('X-File-Size', files[i].fileSize);
						xhr.send(files[i]); // 将file对象发送到服务器端 
					}
				},
				// 用户选择文件时处理文件上传
				handleFile:function(e) {
					this.uploadFiles(e.files);
				},
				showProgress:function(e){
					console.log("showProgress");
					$(".loader div:eq("+this.i1i+")").css({width: Math.round(e.loaded/e.total*100)+"%"});
				},
				finishLoad:function(){
					console.log("finishLoad");
					$(".loader div:eq("+this.i1i+")").css({"background-color": "green"});
				},
				showAjaxProgress:function(){
					console.log("showAjaxProgress");
				},
				finishAjaxLoad:function(){
					console.log("finishAjaxLoad");
				},
				ready:function(){
					console.log("ready"+this.readyState);
					if (this.readyState != 4){
						return;
					}
					$("#message").append("<div class=\"data_content\">成功上传文件,文件内容:<hr /><pre>" + this.responseText + "</pre></div>");
				}
			};
			
            window.onload=function () {
                var div = $("#div").get(0);
                div.ondragenter = function (e) {
					console.log("ondragenter");
                    e.dataTransfer.dropEffect = 'copy'; // 让鼠标出现复制文件时的样式
					e.target.style.background = "yellow";
					e.preventDefault();
                    return false;
                };
                div.ondragover = function (e) {
					console.log("ondragover");
                    e.dataTransfer.dropEffect = 'copy'; // 让鼠标出现复制文件时的样式
					e.target.style.background = "yellow";
					e.preventDefault();
                    return false;
                };
                div.ondrop = function (e) {
					console.log("ondrop");
					var files=e.dataTransfer.files;
					FileManager.uploadFiles(files); // 文件可以通过鼠标的MouseEvent传递过来
					
					e.target.style.background = "lightgray";
					console.log("ondrop");
					e.preventDefault();
                    return false;
                };
				div.ondragleave = function(e) {
					console.log("ondragleave");
					e.target.style.background = "lightgreen";
					e.preventDefault();
                    return false;
				};
				document.ondragstart = function(e) {
					console.log("ondragstart");
					e.preventDefault();
                    return false;
				};
				document.ondragend = function(e) {
					console.log("ondragend");
					e.preventDefault();
                    return false;
				};
				document.ondrag = function(e) {
					console.log("ondrag");
					e.preventDefault();
                    return false;
				};
            }
        </script>
    </head>
    <body>
		<h1>托拽文件上传 Chrome</h1>
		<div id="way">
        	<div id="div" class="dropzone">将文件托放到这里(支持多文件)</div>
		</div>
		<div id="message"></div>
    </body>
</html>
package jumkey.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Upload extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Upload() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.getWriter().println("get");
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		System.out.println(request.getParameter("test"));
		System.out.println(request.getHeader("Content-Type"));
		int v;
		String dir = "//C:/upload";
		String filePath = dir + "/" + URLDecoder.decode(request.getHeader("X-File-Name"),"UTF-8");
		InputStream inputStream = request.getInputStream();
		OutputStream outputStream = new FileOutputStream(new File(filePath));
		byte[] bytes = new byte[4096];
		while ((v = inputStream.read(bytes)) > 0) {
			outputStream.write(bytes, 0, v);
		}
		outputStream.close();
		inputStream.close();
		response.getWriter().println("{\"file\":\""+URLDecoder.decode(request.getHeader("X-File-Name"),"UTF-8")+"\"}");
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

代码可能有点丑,这个只能拖拽上传多个文件而不能上传文件夹。查了api也没发现操作文件夹的。

0
张江男
张江男

applet能做的事就多了, 但是需要购买证书。 如果是自己用的话也可以使用自签名的证书。 

返回顶部
顶部