圧縮パッケージをアップロードして解析します.
37566 ワード
仕事では、圧縮パッケージをアップロードし、圧縮パッケージの指定ファイルを探して、ファイルを解析する必要があります.後で確認するために記録を作成します.
html数:
html数:
<form class="form-horizontal" id="package_form" action="/packageUpload" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="package" class="col-md-2 control-label">zip/rar uploadlabel>
<div class="col-md-3">
<input type="file" id="package" name="package" class="btn btn-default" >
div>
<span id="packageMsg" style="color:#F00;margin-left: 80px;">span>
div>
<div class="form-group">
<div class="col-md-offset-2">
<button type="button" class="btn btn-info" onclick="commitPackageForm()" style="margin-left: 15px;">package button>
<input type="submit" class="btn btn-info" value="package " style="margin-left: 15px;" />--%>
div>
div>
form>
js:function commitPackageForm() {
//
var file = $("#package").val();
if(file == ''){
$("#packageMsg").text(' ');
return;
}
$("#package_form").submit();
}
Propties:spring.servlet.multipart.enabled=true
Config:package com.cykj.reports.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
/**
*
*/
@Configuration
public class FileConfig {
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(524288000); //20M=20*1024k=20*1024*1024b 500M
multipartResolver.setMaxInMemorySize(104857600); //100M
return multipartResolver;
}
}
コントローラ:/**
* package
* @param file
* @param model
* @param request
* @return
*/
@RequestMapping(value = "/packageUpload")
public String packageUpload(@RequestParam("package") MultipartFile file, Model model, HttpServletRequest request){
long start = System.currentTimeMillis();
// , xml , xml list
//fail incomplete xml pass done
XmlParseUtil xpu = gmsService.handlerUpload(file, request);
// list, gmsXml
packingResponseData(xpu, model);
long end = System.currentTimeMillis();
model.addAttribute("msg"," ! :"+ (end-start) + "ms!");
return "gms_result";
}
ServiceImpl:/**
* 1.
* @param file
* @param request
* @return
*/
@Override
public XmlParseUtil handlerUpload(MultipartFile file, HttpServletRequest request) {
if (file.isEmpty()){
return null;
}
// 1. , xml list
UnPackUtil unPackUtil = new UnPackUtil();
AjaxList<String> ajaxList = unPackUtil.handleFile(file, request);
// xml
List<File> fileList = unPackUtil.getFileList();
//2. xml
XmlParseUtil xpu = dataParseAndHandle(fileList);
return xpu;
}
関連するツールクラスの方法:
private List<File> fileList;
private static final String[] extensionPermit = { "zip", "rar" };
/**
*
* @param file
* @param request
* @return AjaxList
*/
public AjaxList<String> handleFile(MultipartFile file, HttpServletRequest request) {
String saveDirectoryPath = request.getContextPath() + File.separator + "uploadFiles";
String saveZipPath = saveDirectoryPath + "/zips/";
String saveRarPath = saveDirectoryPath + "/rars/";
String saveProjectPath = saveDirectoryPath + "/decompressFiles/";
File saveZipDirectory;
File saveRarDirectory;
try {
String fileName = file.getOriginalFilename();
System.out.println(fileName);
String fileExtension = FilenameUtils.getExtension(fileName);
if (!ArrayUtils.contains(extensionPermit, fileExtension)) {
return AjaxList.createFail(" ");
}
String a[] = fileName.split("\\.");
String saveDecompressPath = a[0];
String saveDecompressType = a[1];
System.out.println(a[0] + "******" + a[1]);
if (saveDecompressType.equals("zip")) {
saveZipDirectory = new File(saveZipPath);
if (!saveZipDirectory.exists()) {
saveZipDirectory.mkdirs();
}
System.out.println("saveZipDirectory*****Path:" + saveZipDirectory.getAbsolutePath());
//
File toFile = new File(saveZipDirectory.getAbsolutePath() + File.separator + fileName);
file.transferTo(toFile);
fileList = DecompressUtils.zipToFile(saveZipPath + fileName, saveProjectPath);
} else if (saveDecompressType.equals("rar")) {
saveRarDirectory = new File(saveRarPath);
if (!saveRarDirectory.exists()) {
saveRarDirectory.mkdirs(); //
}
System.out.println("saveRarDirectory*****Path:" + saveRarDirectory.getAbsolutePath());
File toFile = new File(saveRarDirectory.getAbsolutePath() + File.separator + fileName);
file.transferTo(toFile); //
fileList = DecompressUtils.rarToFile(saveRarPath + fileName, saveProjectPath);
}
} catch (Exception e) {
return AjaxList.createFail(" , !");
}
return AjaxList.createSuccess(" , ");
}
戻り値処理:package com.cykj.reports.domain.main;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Description:
* @Date: 2019/1/22
* @Auther:
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class AjaxList<T> {
private boolean isSuccess;
private T data;
public static <T> AjaxList<T> createSuccess(T data) {
return new AjaxList<T>(true, data);
}
public static <T> AjaxList<T> createFail(T data) {
return new AjaxList<T>(false, data);
}
}