添加依赖
太多忘记哪个了。。。添加就完事儿了
$("#file").change(function(){
var objUrl =getObjectURL(this.files[0]);
console.log("objUrl = "+objUrl);
});
function getObjectURL(file)
{
var url =null ;
if (window.createObjectURL!=undefined)
{// basic
url =window.createObjectURL(file);
}
else if (window.URL!=undefined)
{
// mozilla(firefox)
url =window.URL.createObjectURL(file);
}
else if (window.webkitURL!=undefined) {
// webkit or chrome
url =window.webkitURL.createObjectURL(file);
}
return url;
}
前端页面
后端代码
@RequestMapping("/up")
@ResponseBody
public StringimportTO7xzAnchor(@RequestParam(value ="file",required =false) MultipartFile file, HttpServletRequest request)throws IOException, BiffException {
List list =new ArrayList<>();
XSSFWorkbook workbook =null;
// String originalFilename = file.getOriginalFilename();
// System.out.println(s);
String bbbb = file.getOriginalFilename();
if("".equals(bbbb)){
return JsonUtil.toERROR("还没选择上传的Excel文件");
}
String path= PathUtil.getRootClassPath()+ File.separator +"static";(把文件放在target的static下)
String name = UUID.randomUUID().toString().replaceAll("-", "");
String ext ="";
String newName = bbbb + ext;
File newFile =new File(path + File.separator + newName);
file.transferTo(newFile);
FileInputStream fileInputStream =new FileInputStream(newFile);
MultipartFile multipartFile =new MockMultipartFile(newFile.getName(), newFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
// File file2 = multipartToFile(file);
// String name = file.getName();
/* CommonsMultipartFile cFile = (CommonsMultipartFile) file;
DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();
File fo = fileItem.getStoreLocation();*/
// List list1 = new ArrayList();
try {
Workbook rwb=Workbook.getWorkbook(new File(path+bbbb));
Sheet rs=rwb.getSheet(0);//表
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行
System.out.println("表的列数:"+clos+" 表的行数:"+rows);
for (int i =1; i < rows; i++) {
for (int j =0; j < clos; j++) {
//第一个是列数,第二个是行数
String id=rs.getCell(j++, i).getContents();//默认最左边编号也算一列 所以这里得j++
String taglabelName=rs.getCell(j++, i).getContents();
String taglabelClick=rs.getCell(j++, i).getContents();
String taglabelRelate=rs.getCell(j++, i).getContents();
String taglabelUrl = rs.getCell(j++, i).getContents();
String createTime =rs.getCell(j++, i).getContents();
String remark =rs.getCell(j++, i).getContents();
String rem =rs.getCell(j++, i).getContents();
// System.out.println("id:"+id+" taglabelName:"+taglabelName+" taglabelClick:"+taglabelClick+" taglabelRelate:"+taglabelRelate+" taglabelUrl:"+taglabelUrl+" createTime:"+createTime+" remark:"+remark+" rem:"+rem);
// list.add(new TO7xzAnchor(id,s_name,url,click,createTime,remark));
TO7xzTaglabel to7xzAnchor =new TO7xzTaglabel();
if ("".equals(id)){
}else {
to7xzAnchor.setId(Integer.parseInt(id));
}
to7xzAnchor.setTaglabelName(taglabelName);
int a=Integer.valueOf(taglabelClick).intValue();
to7xzAnchor.setTaglabelClick(a);
int a2=Integer.valueOf(taglabelRelate).intValue();
to7xzAnchor.setTaglabelRelate(a2);
to7xzAnchor.setTaglabelUrl(taglabelUrl);
if ("".equals(createTime)||createTime==null){
to7xzAnchor.setCreateTime(new Date());
}else {
to7xzAnchor.setCreateTime( DateUtil.parseDate(createTime));
}
to7xzAnchor.setRemark(remark);
to7xzAnchor.setRem(rem);
list.add(to7xzAnchor);
}
}
List to7xzTaglabels =to7xzTaglabelMapper.selectAll();
for (TO7xzTaglabel to7xzTaglabel : to7xzTaglabels) {
to7xzTaglabelMapper.delete(to7xzTaglabel);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int insert=0;
for (TO7xzTaglabel to7xzAnchor : list) {
if (to7xzAnchor.getId()!=null){
to7xzTaglabelMapper.deleteByPrimaryKey(to7xzAnchor.getId());
}
insert =to7xzTaglabelMapper.insert(to7xzAnchor);
}
if(insert>0)
return JsonUtil.toSUCCESS("上传成功","tagLabel-tab",false);
return JsonUtil.toSUCCESS("上传成功");
}