您的位置:首页 > 编程语言 > Java开发

java上传excel文件,并判断内容大小,文件格式

2017-03-17 17:06 2316 查看
// 确定按钮
$(".confirm").click(function() {
var fileName = $("#uploadFile").val();
var index = fileName.lastIndexOf(".");
var suffix = fileName.substring(index).toLowerCase();

// 验证文件格式
if(fileName == "" || !(".xlsx" == suffix || ".xls" == suffix)) {
$("#excel-error-tip").html("( 文件格式不对,请选择xls或xlsx格式! )").css("display", "");
return false;
}

$.ajaxFileUpload({
url: "$!{urlTool.getExcelPublishFactoryResourceURL('EXCEL_IMPORT_FACTORYRESOURCE')}?factoryId=$!{returnMap.steelFactory.id}&randomNum="+$("#randomNum", parent.document).val(),
type: 'post',
timeout: 300 * 1000,
async: false,
secureuri: false,
fileElementId: 'uploadFile',
success: function(msg) {},
error: function(msg) {
$.popDialog.warning("程序发生错误,请联系技术人员!");
}
});

$(".cont2").show().siblings().hide();
runProcessBar();
return false;
});


下面是JAVA 后台代码

@RequestMapping(value = ExcelPublishFactoryResourceURL.EXCEL_IMPORT_FACTORYRESOURCE, method = RequestMethod.POST)
@ResponseBody
public void excelPublish(HttpServletRequest request, MultipartFile uploadFile, @RequestParam(required = true) String factoryId, String randomNum, HttpServletResponse response) throws IOException
{
File outFile = null;
try
{
BaseEmployeeLoginContext context = (BaseEmployeeLoginContext) request.getSession().getAttribute(BaseEmployeeLoginContext.LOGIN_CONTEXT_NAME);

Map<String, Object> returnMap = new HashMap<String, Object>();

if (uploadFile != null)
{
// 校验file本身 如果file本身错误 则无需再进行数据校验
long size = uploadFile.getSize();
// 文件大小超过2M
if (size > 2097152)
{
// flag 0:上传文件符合规范 1:上传文件大小超过2M 2:上传资源总数量超过200条 3:上传资源超过500条
returnMap.put("status", false);
returnMap.put("message", "资源解析失败,导入文件超过2M!");
}
else
{
byte[] data = new byte[(int) size];
InputStream input = uploadFile.getInputStream();
input.read(data);

File folder = new File(super.getSession().getServletContext().getRealPath("/") + "inventory/");
if (!folder.exists())
{
folder.mkdir();
}

String uploadFileName = uploadFile.getOriginalFilename();

String ExName = uploadFileName.substring(uploadFileName.lastIndexOf("."), uploadFileName.length());

outFile = new File(super.getSession().getServletContext().getRealPath("/") + "inventory/" + factoryId + ExName);

if (!outFile.exists())
{
outFile.createNewFile();
}

FileOutputStream outStream = new FileOutputStream(outFile);

outStream.write(data);
outStream.close();
input.close();

try
{
returnMap = importExcelFactoryResourceAO.publishFactoryResourceByExcel(outFile, NumberUtils.parseLong(factoryId), randomNum, context.getLoginUID(), context.getLoginName());
}
catch (ExcelParseException e)
{
returnMap.put("status", false);
returnMap.put("message", e.getMessage());
}
catch (Exception e)
{
returnMap.put("status", false);
returnMap.put("message", "资源发布失败:文件解析异常...");
}
}
}
else
{
returnMap.put("status", false);
returnMap.put("message", "资源发布失败:资源文件未找到...");
}

// 将资源解析返回信息放入Session中
request.getSession().setAttribute("importMsg", JsonUtils.toJsonString(returnMap));

// 更新进度为100%
abstractRedisCache.set("precent" + randomNum, "100", 120);

response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain; charset=UTF-8");

PrintWriter out = response.getWriter();

out.print(JsonUtils.toJsonString(returnMap));
out.close();
}
catch (Exception e)
{
}
finally
{
// 修改,删除下载下来的excel文件
if (outFile != null)
{
try
{
outFile.getAbsoluteFile().delete();
}
catch (Exception e)
{

}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java excel
相关文章推荐