「java上传xls」java上传文件到指定目录

博主:adminadmin 2023-01-12 07:06:09 728

本篇文章给大家谈谈java上传xls,以及java上传文件到指定目录对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java如何实现将excel文件(.xls)从一个服务器上传到另一台服务器

用java如何实现将excel文件(.xls)从一个服务器上传到另一台服务器 和

用java如何实现将文件从一个服务器上传到另一台服务器 有什么区别吗?

这两台服务器又分别是什么操作系统?是通过页面操作传送,还是后台传送?

java上传excel无法兼容ie11和chrome

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

title批量录入excel表格/title

meta http-equiv="content-type" content="text/html; charset=UTF-8"

/head

script type="text/javascript"  

    //FX获取文件路径方法  

    function readFileFirefox(fileBrowser) {  

        try {  

            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  

        }   

        catch (e) {  

            alert('无法访问本地文件,由于浏览器安全设置,请用IE浏览器打开。');  

            return;  

        }  

        var fileName=fileBrowser.value; //这一步就能得到客户端完整路径。下面的是否判断的太复杂,还有下面得到ie的也很复杂。  

        var file = Components.classes["@mozilla.org/file/local;1"]  

            .createInstance(Components.interfaces.nsILocalFile);  

        try {  

            file.initWithPath( fileName.replace(/\//g, "\\\\") );  

        }  

        catch(e) {  

            if (e.result!=Components.results.NS_ERROR_FILE_UNRECOGNIZED_PATH) throw e;  

            alert("File '" + fileName + "' cannot be loaded: relative paths are not allowed. Please provide an absolute path to this file.");  

            return;  

        }  

        if ( file.exists() == false ) {  

            alert("File '" + fileName + "' not found.");  

            return;  

        }  

      

      return file.path;  

    }  

      

    //根据不同浏览器获取路径  

    function getvl(){  

    //判断浏览器  

      var Sys = {};   

      var ua = navigator.userAgent.toLowerCase();   

      var s;   

      (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :   

      (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :   

      (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :   

      (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :   

      (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;  

      var file_url="";  

      if(Sys.ie="6.0"){  

        file_url = document.getElementById("file").value;  

      }else if(Sys.ie="7.0"){  

        var file = document.getElementById("file");  

        file.select();  

        file_url = document.selection.createRange().text;  

      }else if(Sys.firefox){  

        file_url = readFileFirefox(document.getElementById("file"));  

      }  

      document.getElementById("filepath").value = file_url.replace(/\\/g,'/');  //把\转化成/

      var files = document.getElementById("filepath").value

      

      if(files.substr(files.length-4)=="xlsx" || files.substr(files.length-4)==".xls"){

  document.frm.action = "import_result.jsp";

  document.frm.submit();

  }else{

  alert("请选择office 2007或 office 2003; 以xlsx(xls)结尾...");

  return false;

  }

      

    }  

    /script  

form action="" method="post" name="frm" onsubmit="getvl();" target="import_result"

数据的批量导入:input type="file" id="file" style="height: 4%"/  

    input type="hidden" id="filepath" name="filepath" value="" /

    input type="button" onclick="getvl();" value="Excel导入" style="height: 4%"/

/form

/html

1.如何将客户端的 xls 上传到 服务器 2.将刚刚的文件导入服务器SqlServer 3.服务端用java

xls只是一个文件而已,前台将文件上传到服务器即可,

在服务器得到该上传文件的数据流,序列化后存入数据库就行了

不过为什么要存到数据库去呢,你可以存到硬盘中,在数据库中存这个文件在硬盘中的地址就行了

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上传xls和java上传文件到指定目录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。