java.unrar的简单介绍

博主:adminadmin 2023-01-04 08:36:07 953

本篇文章给大家谈谈java.unrar,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java上传xls文件到服务器后打开提示发现不可读取的内容

页面jsp文件

form name="add" action="/gxtWeb/lvjcontacts/manyContacts.action" method="post"enctype="multipart/form-data" onsubmit="return checkNull();"

s:file name="upload"/s:file

input type="submit" value="导入信息"

/form

action处理:

public class ManyContactsAction extends ActionSupport {

private File upload;

private String uploadContentType;// 要上传的文件的类型

private String uploadFileName;// 要上传的文件

private ContactsManager manager;

private Contacts cbean;

private Persons pbean;

private Long gid;

private String result;

public String execute() throws Exception {

// 检查后缀名是否符合条件,同时更改上传文件的文件名

int filesize = this.getUploadFileName().length();

String fileEx = this.getUploadFileName().substring(

this.getUploadFileName().indexOf("."), filesize);

//获取文件名

String fileName=uploadFileName.substring(0,uploadFileName.indexOf("."));

// 获得上传路径

String realPath = ServletActionContext.getServletContext().getRealPath(

"/UploadFile/");

File saveFile=null;

if (upload != null) {

// 修改文件名,使上传后不至于重复替代

// this.uploadFileName = new Date().getTime() + fileEx;

saveFile = new File(new File(realPath), uploadFileName);

if (!saveFile.getParentFile().exists()) {

saveFile.getParentFile().mkdirs();

}

FileUtils.copyFile(upload, saveFile);// 到这里,文件已上传成功

// 下面进行判断文件是否是rar文件,是就需要解压

if (fileEx.equals(".rar")) {

System.out.println("saveFile:" + saveFile);//rar文件所在保存路径

System.out.println("realPath:" + realPath);//解压后保存路径

// 定义解压字符串,用于解压上传的rar文件,注意此处需要一个unrar.exe文件

String rarpath = ServletActionContext.getServletContext()

.getRealPath("/rarFile/UNRAR.exe x -t -o+ -p- \"");

String jieya = rarpath + saveFile + "\" \"" + realPath + "\"";

Process p1 = Runtime.getRuntime().exec(jieya);// 将传输的rar文件解压

p1.waitFor();

p1.destroy();

FileUtils.deleteQuietly(saveFile);// 删除rar文件

saveFile=new File(new File(realPath),fileName+".xls");

System.out.println("解压后:"+saveFile);

ServletActionContext.getResponse().getWriter().println(

"success!!");

}

if (fileEx.equals(".xls") || fileEx.equals(".xlsx")) {

// 开始读取文件了,获得第一列手机号码

Workbook persons = Workbook.getWorkbook(saveFile);// 获得xls文件

Sheet sheet = persons.getSheet(0);// 获得第一个工作簿

System.out.println("列数:" + sheet.getColumns());

int count = sheet.getRows();// 取得记录数,count行

String cphone;

// 遍历行,获得列数据

for (int i = 0; i count; i++) {

cphone = sheet.getCell(0, i).getContents();// 第一列的所有行

pbean = manager.getPerson(cphone);// 获得该用户,查询别的信息

cbean = new Contacts();

Long contactsid = pbean.getId();

Long pid = 10002L;

cbean.setContactsid(contactsid);// 联系人id

cbean.setPid(pid);// 用户本身的PID

cbean.setCid("cid");// 关系的学校ID

cbean.setGid(gid);// 分组id

manager.addPerson(cbean);

this.contactsLog.writeLog("10002", "批量添加联系人", "批量添加联系人操作","");

System.out.println("添加成功!");

}

}

return SUCCESS;

} else {

return INPUT;

}

}

}

xml配置:

action name="manyContacts" class="manyContactAction"

result name="success" type="redirectAction"personInfo.action

/result

result name="input" type="redirectAction"showmangpersons.action

/result

/action

注意:在文件解压时,需要一个unrar.exe文件,这个文件应保存在工程相应的目录下,我就放在webapps里面了,

按照上面的路径即可获得解压文件并进行解压。

这里配置文件没写任何限制,拦截器之类,都在action里面进行判断了,这里只做了类型判断,没有别的。

[java] view plain copy

页面jsp文件

form name="add" action="/gxtWeb/lvjcontacts/manyContacts.action" method="post" enctype="multipart/form-data" onsubmit="return checkNull();"

s:file name="upload"/s:file

input type="submit" value="导入信息"

/form

action处理:

public class ManyContactsAction extends ActionSupport {

private File upload;

private String uploadContentType;// 要上传的文件的类型

private String uploadFileName;// 要上传的文件

private ContactsManager manager;

private Contacts cbean;

private Persons pbean;

private Long gid;

private String result;

public String execute() throws Exception {

// 检查后缀名是否符合条件,同时更改上传文件的文件名

int filesize = this.getUploadFileName().length();

String fileEx = this.getUploadFileName().substring(

this.getUploadFileName().indexOf("."), filesize);

//获取文件名

String fileName=uploadFileName.substring(0,uploadFileName.indexOf("."));

// 获得上传路径

String realPath = ServletActionContext.getServletContext().getRealPath(

"/UploadFile/");

File saveFile=null;

if (upload != null) {

// 修改文件名,使上传后不至于重复替代

// this.uploadFileName = new Date().getTime() + fileEx;

saveFile = new File(new File(realPath), uploadFileName);

if (!saveFile.getParentFile().exists()) {

saveFile.getParentFile().mkdirs();

}

FileUtils.copyFile(upload, saveFile);// 到这里,文件已上传成功

// 下面进行判断文件是否是rar文件,是就需要解压

if (fileEx.equals(".rar")) {

System.out.println("saveFile:" + saveFile);//rar文件所在保存路径

System.out.println("realPath:" + realPath);//解压后保存路径

// 定义解压字符串,用于解压上传的rar文件,注意此处需要一个unrar.exe文件

String rarpath = ServletActionContext.getServletContext()

.getRealPath("/rarFile/UNRAR.exe x -t -o+ -p- \"");

String jieya = rarpath + saveFile + "\" \"" + realPath + "\"";

Process p1 = Runtime.getRuntime().exec(jieya);// 将传输的rar文件解压

p1.waitFor();

p1.destroy();

FileUtils.deleteQuietly(saveFile);// 删除rar文件

saveFile=new File(new File(realPath),fileName+".xls");

System.out.println("解压后:"+saveFile);

ServletActionContext.getResponse().getWriter().println(

"success!!");

}

if (fileEx.equals(".xls") || fileEx.equals(".xlsx")) {

// 开始读取文件了,获得第一列手机号码

Workbook persons = Workbook.getWorkbook(saveFile);// 获得xls文件

Sheet sheet = persons.getSheet(0);// 获得第一个工作簿

System.out.println("列数:" + sheet.getColumns());

int count = sheet.getRows();// 取得记录数,count行

String cphone;

// 遍历行,获得列数据

for (int i = 0; i count; i++) {

cphone = sheet.getCell(0, i).getContents();// 第一列的所有行

pbean = manager.getPerson(cphone);// 获得该用户,查询别的信息

cbean = new Contacts();

Long contactsid = pbean.getId();

Long pid = 10002L;

cbean.setContactsid(contactsid);// 联系人id

cbean.setPid(pid);// 用户本身的PID

cbean.setCid("cid");// 关系的学校ID

cbean.setGid(gid);// 分组id

manager.addPerson(cbean);

this.contactsLog.writeLog("10002", "批量添加联系人", "批量添加联系人操作", "");

System.out.println("添加成功!");

}

}

return SUCCESS;

} else {

return INPUT;

}

}

}

xml配置:

action name="manyContacts" class="manyContactAction"

result name="success" type="redirectAction"personInfo.action

/result

result name="input" type="redirectAction"showmangpersons.action

/result

/action

注意:在文件解压时,需要一个unrar.exe文件,这个文件应保存在工程相应的目录下,我就放在webapps里面了,

按照上面的路径即可获得解压文件并进行解压。

这里配置文件没写任何限制,拦截器之类,都在action里面进行判断了,这里只做了类型判断,没有别的。

java解压RAR文件的问题

进程冲突吧?

两个命令一起执行的应该。。两个rar进程应该可以同事啊~

Java调用cmd命令不能打开文件

unrar命令在window环境变量里有路径信息,所以在命令提示符里能找到并运行unrar这个命令,到java程序里就没有路径信息,反以就找不到了,要加上unrar命令的绝对路径才能找到。比如:如果unrar命令在c盘下就这样写

runtime.getruntime().exec("c:\\unrar

x

e:\1.rar

e:\");

在LINUX下 用JAVA如何解压rar文件

楼主试试这个代码~~

package decompress;

import java.io.File;

import java.io.FileOutputStream;

import org.apache.tools.ant.Project;

import org.apache.tools.ant.taskdefs.Expand;

import de.innosystec.unrar.Archive;

import de.innosystec.unrar.rarfile.FileHeader;

public class DeCompressUtil {

/**

* 解压zip格式压缩包

* 对应的是ant.jar

*/

private static void unzip(String sourceZip,String destDir) throws Exception{

try{

Project p = new Project();

Expand e = new Expand();

e.setProject(p);

e.setSrc(new File(sourceZip));

e.setOverwrite(false);

e.setDest(new File(destDir));

/*

ant下的zip工具默认压缩编码为UTF-8编码,

而winRAR软件压缩是用的windows默认的GBK或者GB2312编码

所以解压缩时要制定编码格式

*/

e.setEncoding("gbk");

e.execute();

}catch(Exception e){

throw e;

}

}

/**

* 解压rar格式压缩包。

* 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar

*/

private static void unrar(String sourceRar,String destDir) throws Exception{

Archive a = null;

FileOutputStream fos = null;

try{

a = new Archive(new File(sourceRar));

FileHeader fh = a.nextFileHeader();

while(fh!=null){

if(!fh.isDirectory()){

//1 根据不同的操作系统拿到相应的 destDirName 和 destFileName

String compressFileName = fh.getFileNameString().trim();

String destFileName = "";

String destDirName = "";

//非windows系统

if(File.separator.equals("/")){

destFileName = destDir + compressFileName.replaceAll("\\\\", "/");

destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));

//windows系统

}else{

destFileName = destDir + compressFileName.replaceAll("/", "\\\\");

destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));

}

//2创建文件夹

File dir = new File(destDirName);

if(!dir.exists()||!dir.isDirectory()){

dir.mkdirs();

}

//3解压缩文件

fos = new FileOutputStream(new File(destFileName));

a.extractFile(fh, fos);

fos.close();

fos = null;

}

fh = a.nextFileHeader();

}

a.close();

a = null;

}catch(Exception e){

throw e;

}finally{

if(fos!=null){

try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();}

}

if(a!=null){

try{a.close();a=null;}catch(Exception e){e.printStackTrace();}

}

}

}

/**

* 解压缩

*/

public static void deCompress(String sourceFile,String destDir) throws Exception{

//保证文件夹路径最后是"/"或者"\"

char lastChar = destDir.charAt(destDir.length()-1);

if(lastChar!='/'lastChar!='\\'){

destDir += File.separator;

}

//根据类型,进行相应的解压缩

String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);

if(type.equals("zip")){

DeCompressUtil.unzip(sourceFile, destDir);

}else if(type.equals("rar")){

DeCompressUtil.unrar(sourceFile, destDir);

}else{

throw new Exception("只支持zip和rar格式的压缩包!");

}

}

}

关于java.unrar和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。