<script type="text/javascript" src="<%=path %>/js/jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="<%=path %>/statics/libs/upload/css/fileinput.min.css" />
<script src="<%=path %>/statics/libs/upload/js/fileinput.min.js" type="text/javascript"></script>
<script src="<%=path %>/statics/libs/upload/js/locales/zh.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#mdmUpload').fileinput({
language: 'zh',
uploadUrl: './controller.do?method=aaaa',
allowedFileExtensions: ['xlsx'],
showCaption: false,
showUpload: true,
maxFileCount: 3,
enctype: 'multipart/form-data',
validateInitialCount:true,
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
browseClass: 'btn btn-primary',
uploadExtraData:function (previewId, index) { //传参
var data = {
//"reportGroupId": $('#id').html(), //此处自定义传参
};
return data;
}
});
$("#aaaUpload").on("fileuploaded", function (event, data, previewId, index) {
$('#upload-tip-form-message-aaa').html(data.response.msg);
$('#show_tip_aaa').click();
});
});
</script>
<input type="file" id="aaaUpload" name="aaaUpload" multiple />
public class ExcelUtil {
public static String getUploadPath(HttpServletRequest request) {
String appContext = request.getSession().getServletContext()
.getRealPath("/");
String uploadPath = appContext + "upload" + File.separator;
return uploadPath;
}
public static String getDownloadPath(HttpServletRequest request,String fileName) {
String appContext = request.getSession().getServletContext()
.getRealPath("/");
StringBuffer pathBuilfer=new StringBuffer();
pathBuilfer.append(appContext);
pathBuilfer.append("template");
pathBuilfer.append(File.separator);
pathBuilfer.append(fileName);
return pathBuilfer.toString();
}
public static List<String> getRepeatDataGroup(List<String> list) {
List<String> result = new ArrayList<>();
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
for (int i = 0; i < list.size(); i++) {
if (map.containsKey(list.get(i))) {
List<Integer> lis = map.get(list.get(i));
lis.add(i + 2);
map.put(list.get(i), lis);
} else {
List<Integer> lis = new ArrayList<Integer>();
lis.add(i + 2);
map.put(list.get(i), lis);
}
}
if (map.size() < list.size()) {
for (Map.Entry<String, List<Integer>> tmp : map.entrySet()) {
if (tmp.getValue().size() > 1) {
result.add(tmp.getValue().toString());
}
}
}
return result;
}
}
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
String uploadPath = "";
// File Upload
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next()
.toString());
if (file != null) {
uploadPath = ExcelUtil.getUploadPath(request)
+ file.getOriginalFilename();
try {
file.transferTo(new File(uploadPath));
} catch (Exception e) {
logger.info("Upload Exception ";
tipMsg.append("文件上传失败!");
executeState = false;
}
}
}
}
@RequestMapping(params = "method=exportTemplate")
@ResponseBody
public void exportMdmTemplate(HttpServletRequest request,
HttpServletResponse response) {
String fileName = "Template.xlsx";
String path = ExcelUtil.getDownloadPath(request, fileName);
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="
+ fileName);
InputStream inputStream = null;
OutputStream os = null;
try {
inputStream = new FileInputStream(new File(path));
os = response.getOutputStream();
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e) {
logger.info("Download Template Exception ";
} finally {
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}