【功能】:前台上传文件(txt,xls,xlsx,csv,pdf)五种格式的文件后台jav。。。

更新时间:2023-06-16 06:07:27 阅读: 评论:0

【功能】:前台上传⽂件(txt,xls,xlsx,csv,pdf)五种格
式的⽂件后台jav。。。
描述:前台html上传⽂件到后台解析⽂件。⽂件类型有txt,xls,xlsx,csv,pdf这五种类型,并且要判断⽂件内容是否为空,就是有⽂件,但是⽂件内容的字节为零。
⾸先先配置需要maven依赖。
⽂件需要配置⽂件上传和解析⽂件的maven依赖
<!-- File upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 解析⽂件 -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
通知怎么写<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
</dependency>
<dependency>
<groupId>lbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>net.sf.cssbox</groupId>
<artifactId>pdf2dom</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
<!-- /artifact/org.apache.pdfbox/fontbox -->        <dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.15</version>
</dependency>
<!-- /artifact/org.apache.pdfbox/jempbox -->        <dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.16</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>
需要加上<beans:property name="maxInMemorySize" value="0"></beans:property> 否则上传⽂件会报空指针错误。<!-- ⽂件上传 -->
<beans:bean id="multipartResolver"
class="org.springframework.s.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<beans:property name="defaultEncoding" value="UTF-8"></beans:property>
<!-- 设定⽂件上传的最⼤值5MB,5*1024*1024 -->
<beans:property name="maxUploadSize" value="52428800"></beans:property>
<beans:property name="maxInMemorySize" value="0"></beans:property>
</beans:bean>
html:
设置⼀个隐藏的⽂件上传,另⼀个按钮点击的时候触发隐藏的⽂件上传功能。并且把⽂件的名字显⽰在⽂本框⾥。
<input type="text" class="arch-input-style" id="filetext" readonly="true">
<input type="file" id="NEWFILE" onchange="opt(this)" name="file">
<input type="button" class="btn-main002 arch-marginleft3per" onclick="newfile()" value="選択"/>
<input type="button" id="btn_fileExec" value="プロファイル実⾏" class="btn-main001" onclick="implement('implement')" tabindex="6"> JS:欣的成语
点击按钮触发隐藏的⽂件上传
function newfile()
{
}
把⽂件的名字显⽰在⽂本框⾥⾯
function opt()
{
var file = ElementById("NEWFILE").files[0];
var ff = ElementById("NEWFILE").files[0].name;
$('#filetext').val(ff);
}
敝帚自珍的读音
点击プロファイル実⾏把⽂件传给后台
function implement(modeAction)
{
checkall();
var drop1 =$("input[name='checked']:checked").val();
var file = ElementById("NEWFILE").files[0];
if(drop1!=null)
{
dropped(drop1);
}
nosame();
if(file!=null && (samesign=="1"))
{
eliminate();
}
if((obj.checkallsign=="1") && (obj.droppedsign=="1" || obj.droppedsign=="3") && (samesign=="1") && (obj.eliminatesign=="1" || obj.eliminatesign=="3") &&    {
if(modeAction == 'implement')
{
$('#modeAction').val(modeAction);
var form = ElementById('DRET004Form');
form.submit();
}
}
el
{
return fal;
}
}
function eliminate()
{
var file = ElementById("NEWFILE").files[0];
var formData=new FormData();
formData.append("file",file);
$.ajax({
url: "/piosys/DRET004ELIMINATE",
dataType:"json",
type: "POST",关于绘画的名言
data: formData,
async: fal,
enctype: 'multipart/form-data',
cache: fal,
processData: fal,
contentType: fal,
success: function (data) {
if(data=="0")
{
obj.eliminatesign="0";
alert("⽂件不存在");
}
el if(data=="2")
{
obj.eliminatesign="1";
alert("指定的⽂件为0字节");
}
el
{
obj.eliminatesign="1";
}
},
远新error:function (data) {
obj.eliminatesign="0";
alert("⽂件不存在");
}
});
}
controller:
判断⽂件是否为空,⽂件内容是否为零字节
//检查⽤户简档的⽂件是否存在,并且检查⽂件内容是否为空
@ResponBody()
@RequestMapping("/DRET004ELIMINATE")
public String elimate(MultipartFile multipartFile,HttpServletRequest request,HttpServletRespon respon) throws IOException  {
//把requesr转化成MultipartHttpServletRequest
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//获取前端传的⽂件
MultipartFile file = File("file");
//获取⽂件名字
String filename = OriginalFilename();
//获取后缀名
String suffix = filename.substring(filename.lastIndexOf(".") + 1);
//把MultipartFile转化成File
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi =(FileItem();
File f = fi.getStoreLocation();
//0表⽰⽂件不存在,1表⽰⽂件存在并且⽂件不为空,2表⽰⽂件存在但是⽂件为空
String files = null;
QQ黑名单在哪里看
if(file==null) {
files="0";
return files;
}
el {
if(suffix.equals("txt")) {
if(file.isEmpty())
{
files="2";
return files;
}
el {
files="1";
return files;
}
}
el if (suffix.equals("xls") || suffix.equals("xlsx")) {
InputStream is = new FileInputStream(f);
if(suffix.equals("xls")) {
HSSFWorkbook wb = new HSSFWorkbook(is);
for(int sheetIndex=0;sheetIndex&NumberOfSheets();sheetIndex++)
{
HSSFSheet st = wb.getSheetAt(sheetIndex);
if(st==null)
{
files="2";
return files;
}
for(int rowIndex =0;rowIndex<=st.getLastRowNum();rowIndex++)
{
千斤拨HSSFRow row = st.getRow(rowIndex);
if(row==null)
{
files="2";
return files;
}
>带月亮的诗句

本文发布于:2023-06-16 06:07:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1040689.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:上传   是否   隐藏   内容   获取   需要   触发   点击
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图