将多张图片打包成zip包,一起上传

时间:2024-04-16 18:36:11

1、前端页面

<div class="mod-body" id="showRW" style="text-align: center;font-size: 14px;letter-spacing: 1px;line-height: 24px;padding-bottom: 10px;height:400px;background:#FFFFFF !important;padding-top:140px;">
<table style="width: 100%;background:#FFFFFF !important;">
<tr>
<td style="text-align: center;">
<span>选择类型:</span>
<select name="photoType" id="photoType" style="width:200px;"> </select>
</td>
</tr>
<tr>
<td style="text-align: center;">
<span>选择文件:</span>
<input type="file" style="width:200px;padding: 0 0px !important;" name="importImg" id="importImg">
</td>
</tr>
<tr style="text-align: center;">
<td >
<input id="submitBtn" type="button" class="button" value="提交" style="width:130px;background: #263552;color: #FFF;border: 0px;border-radius: 2px;height: 28px;"/>
</td>
</tr>
<tr style="text-align: center;">
<td >
<div style="display: inline-block;width: 50%;line-height: 30px;text-align: left;padding-left: 550px;">
<span style="color:red;">注意:</span><br>
<span>1、仅支持图片打包成ZIP包,不能直接将文件夹打压缩包</span><br/>
<span>2、上传的每张图片大小须在100K以内</span>
</div> </td>
</tr>
</table>
</div>

  2、js代码

//提交
$("#submitBtn").click(function(){ var photoTypeSelect = $("#photoType").val();
if(photoTypeSelect == -1){
alert("请选择照片墙类型");
return false;
} var importFile = $("#importImg")[0].files[0];
if($("#importImg").val() == ''){
alert("请选择文件!");
return false;
}else{
var fileNameIndex = importFile.name.lastIndexOf(".");
var fileName = importFile.name.substring(fileNameIndex,importFile.name.length);
if((fileName.toLowerCase() != ".zip" )){
alert("文件必须为.zip类型");
window.location.href='#';
return false;
}
}
$("#mask").show();
$("#shDiv1").show();
var formData = new FormData();
formData.append("importFile", importFile);
formData.append("photoTypeSelect", photoTypeSelect);
formData.append("conId",conId);
$.ajax({
url:"<%=basePath%>/Conferences/photoWallAction.do?method=importPhotoWall",
type:"post",
dataType:"json",
data: formData,
dataType:"json",
// 告诉jQuery不要去处理发送的数据
processData: false,
// 告诉jQuery不要去设置Content-Type请求头
contentType: false,
async:false,
success:function(json){
$("#mask").hide();
$("#shDiv1").hide();
if(json.state == 1){
if(confirm('导入成功!')){
window.location.reload();
}
}else{
if(confirm('导入失败!')){
window.location.reload();
}
}
},
error:function(){
if(confirm('导入失败!')){
window.location.reload();
}
}
}) })

  3、后台数据处理

@RequestMapping(params = "method=importPhotoWall",method = RequestMethod.POST)
public void importPhotoWall(Integer conId,Integer photoTypeSelect,HttpServletRequest request,HttpServletResponse response,ModelMap model){
try {
JSONObject result = new JSONObject();
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
MultipartFile mFile = mRequest.getFile("importFile");
String theUrl = request.getScheme()+"://"+request.getServerName()+request.getContextPath()+"/";
String fileUrl = request.getSession().getServletContext().getRealPath("files");
try {
File file11 = new File(fileUrl+"/photoWallZip");
if(!file11.exists()){
file11.mkdirs();
}
File file = new File(fileUrl+"/photoWallZip","zip"+conId+".zip");
mFile.transferTo(file); //解压到目的文件夹
File file1 = new File(fileUrl+"/photoWall/"+conId+"/");
if(!file1.exists()){
file1.mkdirs();
} if(file != null){// 压缩文件夹存在
ZipFile zf = new ZipFile(fileUrl+"/photoWallZip/zip"+conId+".zip",Charset.forName("GBK"));
Enumeration<?> entries = zf.entries();
while(entries.hasMoreElements()){
byte[] bytes = new byte[2048];
int count = -1;
ZipEntry entry = (ZipEntry)entries.nextElement();
String fileName = entry.getName();
String saveUrl = "";
if(entry.isDirectory()){ }else{
fileName = new Date().getTime()+fileName.substring(fileName.lastIndexOf("."), fileName.length());
saveUrl = fileUrl+"/photoWall/"+conId+"/"+fileName;
File file2 = new File(saveUrl);
file2.createNewFile(); //创建文件
InputStream is = zf.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(file2);
BufferedOutputStream bos = new BufferedOutputStream(fos, 2048);
while((count = is.read(bytes)) > -1)
{
bos.write(bytes, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
List<PhotoWall> phoList = photoWallService.getPhotpWallByConIdAndTypeasc(conId, photoTypeSelect);
PhotoWall photoWall = new PhotoWall();
photoWall.setConferencesId(conId);
photoWall.setCreateTime(new Date());
if(phoList != null && phoList.size() > 0){
if(phoList.get(0) != null && phoList.get(0).getLastDoTime() != null){
photoWall.setLastDoTime(new Date(phoList.get(0).getLastDoTime().getTime()+60000));
}else{
photoWall.setLastDoTime(new Date());
}
}else{
photoWall.setLastDoTime(new Date());
}
photoWall.setCreateUserId(-1);
photoWall.setCreateUserType(-1);
photoWall.setImageUrl(theUrl+"files/photoWall/"+conId+"/"+fileName);
photoWall.setLaudCount(0);
photoWall.setState(2);
photoWall.setTypeId(photoTypeSelect);
photoWallService.saveOrUpdateObject(photoWall);
}
zf.close();
if(!file.exists()){
System.out.println("删除文件失败,文件不存在");
}else{
if (file.isFile())
if(!file.delete()){//判断是否删除完毕
System.gc();//系统进行资源强制回收
file.delete();
}
System.out.println("删除成功");
}
}
} catch (Exception e) {
result.accumulate("state", 0);
e.printStackTrace();
}
result.accumulate("state", 1);
writeToJson(response, result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}