读书人

jsp 下传 上载 文件夹功能

发布时间: 2013-02-24 17:58:56 作者: rapoo

jsp 上传 下载 文件夹功能
求教各位 ,jsp实现上传下载 文件 ,文件夹
或者在jsp层面实现将文件夹压缩打包,然后上传到服务器,在服务器进行解压缩,服务器端会有一个临时文件夹来存放这些上传的文件。
同样的道理,将服务器端的文件夹,不压缩,直接下载,如何做?

求高手指导。。。

[解决办法]
文件夹不能直接上传和下载。
如果上传文件夹的话,可以考虑把文件夹里的文件上传到服务器,然后再服务器创建跟本地相同的文件夹结构。。

下载的话,只能把文件压缩成压缩包然后下载。难道你见过有下载文件夹的页面???
[解决办法]
同意楼上说法,文件上传与下载,叫都叫“文件”,换成是文件夹怎么可能。
你可能看到别人的文件夹能上传,其实际他是进行了处理的,他会获得你的文件夹的路径,然后在后台用递归,把文件夹里面的文件都上传到本地。你以为是直接将文件夹上传呀,那是不可能的!
他们的原理都是文件流,文件流,说白了就是对文件的操作。
你现在直接想要上传文件夹或者下载,那是不行的,你要进行处理,去看看递归把,我也没有做个这样的上传!

[解决办法]
首先在java框架里面,如果你先定义好文件传输成功或失败两种状态,文件的批量上传不会因为某个文件传输失败而导致整个传输过程失效,这个可以用程序控制提示哪个文件传输失败,然后另传就可以了。
其次文件的批量下载是可以的,方法正如1楼所说,将要下载的文件压缩后下载就可以了。
[解决办法]


LZ想法很好。我来帮你实现:
首先通过压缩方式:

/***************************************************************************
* 压缩文件
*
* @author Louis
* @param srcfile
* 需要压缩的文件列表
* @param zipfile
* 压缩后的文件
* @author Louis
*/
public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipfile));
// Compress the files
for (int i = 0; i < srcfile.length; i++) {
FileInputStream in = new FileInputStream(srcfile[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(srcfile[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();

} catch (IOException e) {
e.printStackTrace();
}
}

/***************************************************************************
* 解压缩文件
*
* @author Louis
* @param zipfile
* 需要解压缩的文件
* @param descDir
* 解压后的文件目录
* @author Louis
*/
public static void UnZipFiles(java.io.File zipfile, String descDir) {
try {
// Open the ZIP file
ZipFile zf = new ZipFile(zipfile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
// Get the entry name
ZipEntry entry = ((ZipEntry) entries.nextElement());
String zipEntryName = entry.getName();
InputStream in = zf.getInputStream(entry);


// System.out.println(zipEntryName);
OutputStream out = new FileOutputStream(descDir + zipEntryName);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
// Close the file and stream
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 下载服务器中的txt文件
* @param filePath
* 需要下载的文件File包装类
* @param filename
* 下载后默认的文件名
* @param response
*
* @throws Exception
*/
public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;

//File uploadFile = new File(filePath);
File uploadFile = filePath;
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
filename = URLEncoder.encode(filename, "GBK");

//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
}

/**
* 上传文件并返回上传后的文件名
*
* @param uploadFileName
* 被上传的文件名称
* @param savePath
* 文件的保存路径
* @param uploadFile
* 被上传的文件
* @return 成功与否
* @throws IOException
*/
public static String uploadForName(String uploadFileName, String savePath, File uploadFile) throws IOException {
String newFileName = checkFileName(uploadFileName, savePath);
FileOutputStream fos = null;


FileInputStream fis = null;
try {
fos = new FileOutputStream(savePath +"/"+ newFileName);
fis = new FileInputStream(uploadFile);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
throw e;
}
}
return newFileName;
}
******************************************************************************
上面的类只需要LZ根据逻辑组合一下。就能实现文件夹压缩或者解压缩上传下载了。。。

关于下载文件夹的问题。。我能想到的就只能是通过 写一个bat文件。然后通过java程序来调用实现。。


[解决办法]
以前还真做过这样的,首先在客户端压缩,然后加密,然后ftp上传。
服务端不停的扫描某个特定的目录,然后解密,解压,进行数据处理
[解决办法]
上传:
一、zip注意有大小限制(2G)
二、注意服务器类型
如果是winserver的话最好是调用系统的winRaR进行压缩文件的打包。
zip是开源的,java可以找到打包和解包的代码。
rar不是开源的,目前我只知只能通系统调用进行打包。
解包可以通过找工具实现。
如果是linux的话,最好用tar.gz打包
但如果是浏览器上传,一般都是rar或zip。

下载的话(数据导出)
根据需要实现。而且一般大文件最好考虑断点传送,这个不难,楼主可以找找资料。主要利用http头的信息。
上传文件太大或目录格式复杂。如果是管理员操作且有权限,最好是ftp解决。

读书人网 >J2EE开发

热点推荐