后台(java)接收前台文件上传,微信小程序文件上传
后台接收前台文件上传,微信小程序文件上传
因为初次进行一个小项目,出现不少问题,总结一些问题点,便于后期查阅。
一个简单的文件上传,前台提交文件,后台接收处理,ssm框架实现
前端页面用form表单提交,记得加上enctype="multipart/form-data"属性,用于定义传输编码,当然也可以用其他方式进行传输,这里只亲自测试了该种方式可行。
前端html代码:
<form action="fileHandle/getExcel" enctype="multipart/form-data" method="post">
<label>选择文件</label>
<input name="myfile" type="file"/><!-- name属性用于后台接收文件 -->
<input type="submit" value="提交"/>
</form>
事先要配置好spring的配置文件,这里就不多说了。
后端接收文件:
多方百度后用了MultipartFile类接收,用该类需要导入jar包,我用了maven。后面打印文件信息要用到jxl的jar中的一些类,一起把代码粘贴了。
有些文章说还要导入io包,但是fileupload 1.3版本之后就兼容了io包,因此只需导一个就可以了。
maven代码为:
<!-- 文件传输 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
spring-mvc.xml文件增加配置:
<!-- 定义文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
controller层的代码:
接收到了文件,将里面的信息全部打印出来,这里是上传的excel表格,xls格式。
如果是xlsx格式的表格,Workbook 接收会报错,可以将文件打开另存为xls格式。还有一点是如果文件设密码jxl会报异常。
package controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
@Controller
@RequestMapping("fileHandle")
public class FileHandle {
@RequestMapping(value="getExcel")
//这里value值与form中的input上传中的name相对应
public void getFile(HttpServletRequest request,@RequestParam(value="myfile") MultipartFile file){
System.out.println("后台输出:"+file.getOriginalFilename());//打印出文件名
Workbook rwb;
try {
rwb = Workbook.getWorkbook(file.getInputStream());//将MultipartFile类型转成字节流,强转成(File)会报错
Sheet rs=rwb.getSheet(0);
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行
System.out.println(clos+" rows:"+rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < clos; j++) {
//第一个是列数,第二个是行数
//默认最左边编号也算一列 所以这里得j++
System.out.print(rs.getCell(j, i).getContents()+"\t");
}
System.out.println();
}
} catch (BiffException | IOException e) {
e.printStackTrace();
}
}
}
效果图:
前端页面提交
用wps打开的xls文件
上传后控制台显示结果
补充
在后台接收到信息可以对信息进行处理,可保存新文件,也可分类存入数据库。
微信小程序的文件上传
微信小程序的js代码
//从本地挑选文件
wx.chooseMessageFile({
count:1,
type: 'file',
success(res) {
var path = res.tempFiles[0].path;//文件资源地址
wx.showModal({
title: '提示',
content: '确认您的选择:' + res.tempFiles[0].name,
success(res) {
if (res.confirm) {
console.log('用户点击确定')
//将文件传给开发者服务器
wx.uploadFile({
url: 'http://localhost:8080/test/fileHandle/getExcel',//后台接口
header: { "Content-Type": "multipart/form-data" },//类型
filePath: path,//文件路径
name: 'myfile',//文件名
success(res) {
console.log('传输成功')
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})