您的位置:首页 > 编程语言 > Java开发

java web 文件上传处理

2015-07-17 13:54 471 查看
这两天项目新添加一个功能,实现文件上传,并且在服务器端由sqlite3处理上传的.db文件,将数据批量入库。下面记录一下该功能的实现过程:

首先是前端页面form表单

<form name="upfile" action="${request.getContextPath()}/mob/gchat.do" method="post" enctype="multipart/form-data">
<span style="white-space:pre"> </span><table>
<span style="white-space:pre"> </span><tr hidden="hidden">
<td>
<span style="white-space:pre"> </span><input name="kxy" value="<span style="font-family: helvetica, arial, freesans, clean, sans-serif; font-size: 13.3400001525879px; line-height: 20.0100002288818px; background-color: rgb(248, 248, 248);">imei=%s&app=whatsapp&uuid=%s&cid=%s&file=whatsappchat</span>"/>
</td>
</tr>

<tr>
<td width="25%" align="right">上传文件:</td>
<td><input id="file" type="file" NAME="file" style="width:300px;"></td>
<span style="white-space:pre"> </span></tr>
<tr align="center" valign="middle">
<span style="white-space:pre"> </span><td height="60" colspan="2"><input type="submit" ID="BtnOK" value="确认上传"></td>
</tr>
</table>
</form>

实现文件上传功能,form表单需要添加enctype="multipart/form-data"属性。测试页面form表单挺简单的,接着是服务器端功能的实现:
1、创建文件存储路径

String path=request.getSession().getServletContext().getRealPath("/WEB-INF/upload/"+uuid+"/");
File file = new File(path);
if(!file.exists()){
file.mkdirs(); //该路径下不存在,则创建 <pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">}</span>

2、判断文件是不是压缩文件(并处理)

<pre name="code" class="html">if(file.getSize()>0){//file为前台页面传到服务器的 MultipartFile file
<span style="white-space: pre;"> </span>String type = file.getOriginalFileName();
<span style="white-space: pre;"> </span>if(type.endsWith(".db")){<pre name="code" class="html"> <span style="white-space:pre"> </span>FileUtils.copyInputStreamToFile(file.getInputStream(),new File(path, fileName));//如果是.db文件直接存储到服务器指定path
}else if(type.endsWith(".zip")){//压缩包
SystemUtil.unZip(file.getInputStream(),uuid,path,fileName);//将文件解压缩到path路径下
<span style="white-space:pre"> </span>}}


3、具体实现解压文件的功能

解压缩文件分为两种①解压zip也是上面用到的;②解压rar文件 

这里主要实现zip解压,rar文件在其他文章里另作说明
//解压缩文件
/**
*
* @param is
* @param uuid
* @param savepath 路径
* @param fileName .db文件名
*/
public static void unZip(InputStream is,String uuid,String savepath,String fileName) {
int count = -1;
int index = -1;
boolean flag = false;
savepath = savepath + "\\";
File file = new File(savepath);
if(!file.exists()){

<span style="white-space:pre"> </span>file.mkdirs();
}
try
{
BufferedOutputStream bos = null;
ZipEntry entry = null;
FileInputStream fis = (FileInputStream)is;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
while((entry = zis.getNextEntry()) != null) //判断zis是否存在下一个entry
{
if(entry.isDirectory()){//判断entry是不是文件夹
file.mkdirs();
continue;
}
byte data[] = new byte[buffer];

String temp = entry.getName();
flag = isDB(temp);
if(flag){//如果是.db文件,改成默认名字
// temp = "whatsappchat_$"+uuid+".db";
temp = fileName;
}
index = temp.lastIndexOf("/");
if(index > -1)
temp = temp.substring(index+1);
temp = savepath + temp;

File f = new File(temp);
f.createNewFile();

FileOutputStream fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos, buffer);

while((count = zis.read(data, 0, buffer)) != -1)
{
bos.write(data, 0, count);
}

bos.flush();
bos.close();
}

zis.close();

} catch (Exception e) {
e.printStackTrace();
}
}
以上上传文件就处理完了,接着是用sqlite3处理.db文件将数据批量入库,java 通过sqlite3处理.db 文件在http://blog.csdn.net/liang100k/article/details/46829415有介绍,这里就不写具体实现了。

补充:上面的代码能实现文件名称不为中文的解压缩,下面的代码(测试方法)是可以处理中文名称的.zip文件

package UnZip;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

/**
* 可以处理中文文件名
*/
public class UnZip2
{
private static final int buffer = 2048;

public static void main(String[] args)
{
unZip("C:\\Users\\zm\\Desktop\\test.rar");
}

public static void unZip(String path)
{
int count = -1;
int index = -1;
String savepath = "";
boolean flag = false;
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;

savepath = path.substring(0, path.lastIndexOf("\\")) + "\\test\\"; //文件解压缩路径

try
{
ZipFile zipFile = new ZipFile(path);

Enumeration<?> entries = zipFile.getEntries();

while(entries.hasMoreElements())
{
byte buf[] = new byte[buffer];

ZipEntry entry = (ZipEntry)entries.nextElement();

String filename = entry.getName();
index = filename.lastIndexOf("/");
if(index > -1)filename = filename.substring(index+1);
filename = savepath + filename;
file = new File(filename);
file.createNewFile();

is = zipFile.getInputStream(entry);

fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);

while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count );
}

bos.flush();
bos.close();
fos.close();

is.close();
}

zipFile.close();

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web