首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java中怎么实现文件打包上传以及自动解压

2014-01-01 
java中如何实现文件打包上传以及自动解压? ? ? ? if (files.getCount() 1) {? ? ? ? ? ? File myFile

java中如何实现文件打包上传以及自动解压

? ? ? ? if (files.getCount() == 1) {

? ? ? ? ? ? File myFile = upload.getFiles().getFile(0);

? ? ? ? ? ? logger.info("|------upload file path name:" + myFile.getFilePathName());

? ? ? ? ? ? if (myFile.isMissing()) {

? ? ? ? ? ? ? ? throw new UploadFileIsMissingException("上传文件丢失!");

? ? ? ? ? ? }

? ? ? ? ? ? return myFile;

? ? ? ? } else {

? ? ? ? ? ? throw new MultiFileException("必须上传,而且一次只能上传一个文件!");

? ? ? ? }

? ? }

?

? ? //判断文件是否丢失

? ? public boolean exists(File file) {

? ? ? ? return !file.isMissing();

? ? }

?

? ? //保存文件,如果文件不存在,新建目录,文件,(MultiFileException 请自建)

?

? ? public int save(String targetFilePath, int arg1) throws ServletException,

? ? ? ? ? ? IOException {

? ? ? ? try {

? ? ? ? ? ? if (logger.isInfoEnabled())

? ? ? ? ? ? ? ? logger.info("|------save upload file to :" + targetFilePath);

? ? ? ? ? ? File uploadFile = getSingleUploadFile();

? ? ? ? ? ? String fileName = uploadFile.getFileName();

? ? ? ? ? ? this.savePath = targetFilePath + java.io.File.separator + fileName;

? ? ? ? ? ? //按绝对路径保存

? ? ? ? ? ? FileUtils.createNewFile(savePath);

? ? ? ? ? ? uploadFile.saveAs(savePath, arg1);

logger.info("upload file and save is sucessed......!");

? ? ? ? ? ? return 1;

? ? ? ? } catch (MultiFileException e) {

? ? ? ? ? ? throw new IOException(e.getMessage());

? ? ? ? } catch (UploadFileIsMissingException e) {

? ? ? ? ? ? throw new IOException(e.getMessage());

? ? ? ? }

? ? }

?

? ? public String getAbsoluteSavePath() {

? ? ? ? return savePath;

? ? }

?

?

? ? public Hashtable dealAllPara() throws Exception {

? ? ? ? Hashtable hashtable = new Hashtable();

? ? ? ? Enumeration e = request.getParameterNames();

?

? ? ? ? while (e.hasMoreElements()) {

? ? ? ? ? ? String paraname = (String) e.nextElement();

? ? ? ? ? ? String para = request.getParameter(paraname);

? ? ? ? ? ? if ((paraname != null) && (para != null)) {

? ? ? ? ? ? ? ? hashtable.put(paraname, para);

? ? ? ? ? ? }

? ? ? ? ? ? logger.info("UPLOAD/param:name=" + paraname + ",value=" + para);

? ? ? ? }

? ? ? ? logger.info("param length = " + hashtable.size());

? ? ? ? return hashtable;

? ? }

?

? ? public String getFilenameNoExt() throws MultiFileException,

? ? ? ? ? ? UploadFileIsMissingException {

? ? ? ? File file = getSingleUploadFile();

? ? ? ? String fileName = file.getFileName();

? ? ? ? return fileName.substring(0, fileName.lastIndexOf("."));

? ? }

?

? ? public Request getRequest() {

? ? ? ? return upload.getRequest();

? ? }

?

? ? public Files getFiles() {

? ? ? ? return upload.getFiles();

? ? }

}

?

调用上两个类就可以实现文件上传,下面讨论文件解压

?

1. 解压接口

?

package com.commonStrut.utils;

?

?

public interface IZipUtils {

? ? //File zipFile, String unzipDir

? ? public String unzip() throws Exception;

}

?

2. 解压

?

package com.commonStrut.utils;

?

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

?

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

?

/**

?* @author 魏 剑

?* ?

?*/

public class ZipUtils implements IZipUtils {

? ? private Log logger = LogFactory.getLog(ZipUtils.class);

?

? ? private File zipFile;

?

? ? private String unzipDir;

?

? ? public ZipUtils(File zipFile, String unzipDir) {

? ? ? ? this.zipFile = zipFile;

? ? ? ? this.unzipDir = unzipDir;

? ? }

?

? ? public String unzip() throws Exception {

? ? ? ? logger.info("zipFile path=" + zipFile.getPath());

? ? ? ? ZipFile zf = new ZipFile(zipFile);

? ? ? ? Enumeration enu = zf.entries();

? ? ? ? String result = "";

?

? ? ? ? while (enu.hasMoreElements()) {

? ? ? ? ? ? ZipEntry entry = (ZipEntry) enu.nextElement();

? ? ? ? ? ? String name = entry.getName();

?

? ? ? ? ? ? //如果解压entry是目录,直接生成目录即可,不用写入,如果是文件,要讲文件写入

? ? ? ? ? ? String path = unzipDir + name;

? ? ? ? ? ? result = result + path + "<br/>";

? ? ? ? ? ? File file = new File(path);

? ? ? ? ? ? if (entry.isDirectory()) {

? ? ? ? ? ? ? ? file.mkdirs();

? ? ? ? ? ? } else {

?

//建议使用如下方式创建 流,和读取字节,不然会有乱码(当然要根据具体环境来定),具体原因请听

//下回分解,呵呵

? ? ? ? ? ? ? ? InputStream is = zf.getInputStream(entry);

? ? ? ? ? ? ? ? byte[] buf1 = new byte[1024];

? ? ? ? ? ? ? ? int len;

?

? ? ? ? ? ? ? ? if (!file.exists()) {

? ? ? ? ? ? ? ? ? ? file.getParentFile().mkdirs();

? ? ? ? ? ? ? ? ? ? file.createNewFile();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? OutputStream out = new FileOutputStream(file);

? ? ? ? ? ? ? ? while ((len = is.read(buf1)) > 0) {

? ? ? ? ? ? ? ? ? ? String buf = new String(buf1, 0, len);

?

? ? ? ? ? ? ? ? ? ? out.write(buf1, 0, len);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? result ="文件解压成功,解压文件:<br/>" + result;?

? ? ? ? logger.info("----------------unzip msg = "+result);

? ? ? ? return result;

? ? }

}

?

为了实现提交后后台一次处理,需要将上传&解压桥接起来,也写了一个类,有兴趣的朋友可以参考,别打人啊!哎呀,马上就完了

package com.commonStrut.utils.upload;

?

import java.io.File;

import java.io.IOException;

import java.util.Hashtable;

?

import javax.servlet.ServletException;

?

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

?

import com.commonStrut.utils.IZipUtils;

import com.commonStrut.utils.ZipUtils;

import com.commonStrut.utils.ZipUtils;

import com.jspsmart.upload.Files;

import com.jspsmart.upload.Request;

import com.jspsmart.upload.SmartUpload;

?

?

public class ZipUploadAndUnzip implements IZipUtils,IUpload{

?

? ? private Log logger = LogFactory.getLog(ZipUploadAndUnzip.class);

? ? private IZipUtils ziputils = null;

? ? private String uploadTargetFileName = "";

? ? private String unzipDir = "";

? ? private SingleUpload upload = null;

? ??

? ? public ZipUploadAndUnzip(SingleUpload upload, String saveTargetFilePath,

? ? ? ? ? ? String unzipDir) throws ServletException, IOException {

? ? ? ? this.upload = upload;

? ? ? ? this.uploadTargetFileName = saveTargetFilePath;

? ? ? ? this.unzipDir = unzipDir;

? ? }

?

?

? ? public String unzip() throws Exception{

? ? ? ? //保存

? ? ? ? upload.save(uploadTargetFileName,SmartUpload.SAVE_PHYSICAL);

? ? ? ? //得到保存后文件

? ? ? ? String savePath = upload.getAbsoluteSavePath();

? ? ? ? logger.info("-------------absolute save path = "+savePath);

? ? ? ? File file = new File(savePath);

? ? ? ? //解压

? ? ? ? ziputils = new ZipUtils(file,unzipDir);

? ? ? ? return ziputils.unzip();

? ? }

? ? public String getFilenameNoExt() throws MultiFileException, UploadFileIsMissingException {

? ? ? ? return upload.getFilenameNoExt();

? ? }

?

?

?

? ? public com.jspsmart.upload.File getSingleUploadFile() throws MultiFileException, UploadFileIsMissingException {

? ? ? ? return upload.getSingleUploadFile();

? ? }

?

? ??

? ? public boolean exists(com.jspsmart.upload.File file) {

? ? ? ?return upload.exists(file);

? ? }

?

? ? public int save(String targetFileName, int arg1) throws ServletException, IOException {

? ? ? ? return upload.save(targetFileName,arg1);

? ? }

?

? ??

? ? public String getAbsoluteSavePath() {

? ? ? ? return upload.getAbsoluteSavePath();

? ? }

?

? ??

? ? public Request getRequest() {

? ? ? ? return upload.getRequest();

? ? }

?

? ?

? ? public Files getFiles() {

? ? ? ? return upload.getFiles();

? ? }

?

? ??

? ? public Hashtable dealAllPara() throws Exception {

? ? ? ?return upload.dealAllPara();

? ? }

}

?

有朋友说了,你既然这么罗嗦了,干脆把如何调用的也给个例子吧,好好好,盛情难却啊

假如你是用 struts,可以在Action类如下调用

?

public class UploadAction extends Action{

? ? ?public ActionForward execute(ActionMapping mapping, ActionForm actionForm,

? ? ? ? ? ? HttpServletRequest request, HttpServletResponse response)

? ? ? ? ? ? throws Exception {

?

? ? ? ? ? ? SmartUpload mySmartUpload = new SmartUpload();

? ? ? ? ? ? mySmartUpload.initialize(this.getServlet().getServletConfig(),

? ? ? ? ? ? ? ? ? ? request, response);

? ? ? ? ? ? SingleUpload singleUpload = new SingleUpload(mySmartUpload);

?

? ? ? ? ? ? //处理参数

?

? ? ? ? ? ? ?Hashtable allPara = singleUpload.dealAllPara();

?

? ? ? ? ? ? ?//文件上传保存路径,&解压后保存的目录,这里是定义在配置文件中,具体实现大家写罗

?

? ? ? ? ? ? String saveTargetFilePath = InitConfiguration.getInstance()

? ? ? ? ? ? ? ? ? ? ? ? .getCONFIG("uploadPicDir");

? ? ? ? ? ? String unzipDir = InitConfiguration.getInstance().getCONFIG(

? ? ? ? ? ? ? ? ? ? ? ? "unzipPicDir");

?

? ? ? ? ? ? //upload and unzip.........

? ? ? ? ? ? String unzipMsg = uploadAndUnzip(bf, singleUpload, allPara,

? ? ? ? ? ? ? ? ? ? ? ? saveTargetFilePath, unzipDir, true);

?

? ? ?}

?

}

?

好了,要吃饭了,写的不好请多多包涵.也请多多指教,可不许骂人

热点排行