use of org.springframework.web.multipart.MultipartFile in project Workload by amoxu.
the class TeachingController method uploadTeaching.
@RequestMapping(value = "/upload/teaching", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
@ResponseBody
public String uploadTeaching(@RequestParam(value = "file", required = false) MultipartFile buildInfo, HttpServletRequest request, HttpServletResponse response) {
if (!buildInfo.isEmpty()) {
String path = request.getSession().getServletContext().getRealPath("/upload") + File.separatorChar + buildInfo.getOriginalFilename();
// 需要先保存在本地
File file;
AjaxResult ajaxResult = new AjaxResult();
List list = null;
file = new File(path);
try {
buildInfo.transferTo(file);
logger.info(path);
// 解析
teachingService.saveAsData(path);
} catch (IOException e) {
e.printStackTrace();
}
}
return "failed";
}
use of org.springframework.web.multipart.MultipartFile in project Workload by amoxu.
the class UploadExcel method importExcel.
@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
@ResponseBody
public String importExcel(@RequestParam(value = "file", required = false) MultipartFile buildInfo, HttpServletRequest request, HttpServletResponse response) {
if (buildInfo != null) {
String path = request.getSession().getServletContext().getRealPath("/upload") + File.separatorChar + buildInfo.getOriginalFilename();
// 需要先保存在本地
File file = null;
AjaxResult ajaxResult = new AjaxResult();
try {
file = ToolKit.getFileFromBytes(buildInfo.getBytes(), path);
} catch (CustomException e) {
e.printStackTrace();
ajaxResult.failed();
ajaxResult.setMsg(e.getMessage());
return ajaxResult.toString();
} catch (IOException e) {
e.printStackTrace();
ajaxResult.failed();
ajaxResult.setMsg(e.getMessage());
return ajaxResult.toString();
}
List list = null;
String type = request.getParameter("type");
System.out.println(type);
try {
switch(type) {
case ExcelServiceImpl.REMOTE_TEACH:
// 这里是解析excel文件
list = excelService.inTeachWorkload(file);
list = remoteTeachService.insertLoadByList(list);
break;
case ExcelServiceImpl.REMOTE_EXP:
// 这里是解析excel文件
list = excelService.inExpWorkLoad(file);
list = remoteExpService.insertLoadByList(list);
break;
case ExcelServiceImpl.REMOTE_DESIGN:
list = excelService.inDesignList(file);
list = designService.insertLoadByList(list);
break;
case ExcelServiceImpl.REMOTE_GRA:
list = excelService.inGraduateWorkload(file);
list = graService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_COURSE:
list = excelService.inLCourseWorkload(file);
list = localCourseService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_EXP:
list = excelService.inLExpWorkload(file);
list = localExpService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_DESIGN:
list = excelService.inLDesignWorkload(file);
list = localDesignService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_GRA_PRACTICE:
list = excelService.inLGraPracticeWorkload(file);
list = localGraPracticeService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_GRA_DESIGN:
list = excelService.inLGraDesignWorkload(file);
list = localGraDesignService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_PRACTICE:
list = excelService.inLPracticeWorkload(file);
list = localPracticeService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_PROJECT:
list = excelService.inLProjectWorkload(file);
list = localProjectService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_MATCH:
list = excelService.inLMatchWorkload(file);
list = localMatchService.insertLoadByList(list);
break;
case ExcelServiceImpl.LOACAL_NET:
list = excelService.inLNetWorkload(file);
list = localNetService.insertLoadByList(list);
break;
case ExcelServiceImpl.EXP:
list = excelService.inExpriment(file);
list = exprimentService.insertLoadByList(list);
break;
case ExcelServiceImpl.REMOTE_NON_LESSON:
list = excelService.inNonLesson(file);
list = nonLessonService.insertLoadByList(list);
break;
default:
}
} catch (Exception e) {
e.printStackTrace();
// 这里做的是一个插入数据库功能
ajaxResult.setMsg(e.getMessage());
ajaxResult.failed();
ajaxResult.setData(list);
return ajaxResult.toString();
}
ToolKit.deleteFile(path);
if (list.size() == 0) {
ajaxResult.ok();
ajaxResult.setMsg("导入成功");
return ajaxResult.toString();
} else {
ajaxResult.failed();
ajaxResult.setMsg("导入失败");
return ajaxResult.toString();
}
}
AjaxResult ajaxResult = new AjaxResult<>();
ajaxResult.failed();
return ajaxResult.toString();
}
use of org.springframework.web.multipart.MultipartFile in project collect by openforis.
the class RecordController method startCsvDataImportJob.
@RequestMapping(value = "survey/{surveyId}/data/csvimport/records", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public JobView startCsvDataImportJob(@PathVariable("surveyId") int surveyId, @RequestParam("file") MultipartFile multipartFile, @RequestParam String rootEntityName, @RequestParam String importType, @RequestParam String steps, @RequestParam(required = false) Integer entityDefinitionId, @RequestParam(required = false) boolean validateRecords, @RequestParam(required = false) boolean deleteEntitiesBeforeImport, @RequestParam(required = false) String newRecordVersionName) throws IOException {
File file = Files.writeToTempFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), "ofc_csv_data_import");
CollectSurvey survey = surveyManager.getById(surveyId);
CSVDataImportJob job = jobManager.createJob(TransactionalCSVDataImportJob.class);
CSVDataImportSettings settings = new CSVDataImportSettings();
settings.setDeleteExistingEntities(deleteEntitiesBeforeImport);
settings.setRecordValidationEnabled(validateRecords);
settings.setInsertNewRecords("newRecords".equals(importType));
settings.setNewRecordVersionName(newRecordVersionName);
CSVDataImportInput input = new CSVDataImportInput(file, survey, fromStepNames(steps), entityDefinitionId, settings);
job.setInput(input);
jobManager.start(job);
this.csvDataImportJob = job;
return new JobView(job);
}
use of org.springframework.web.multipart.MultipartFile in project collect by openforis.
the class SurveyController method prepareSurveyImport.
@RequestMapping(value = "prepareimport", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public Response prepareSurveyImport(@RequestParam("file") MultipartFile multipartFile) throws IOException {
String fileName = multipartFile.getOriginalFilename();
File tempFile = Files.writeToTempFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), "ofc_csv_data_import");
String extension = FilenameUtils.getExtension(fileName);
this.uploadedSurveyFile = tempFile;
if (surveyBackupInfoExtractorJob != null && surveyBackupInfoExtractorJob.isRunning()) {
surveyBackupInfoExtractorJob.abort();
}
surveyBackupInfoExtractorJob = jobManager.createJob(SurveyBackupInfoExtractorJob.class);
if (COLLECT_EARTH_PROJECT_FILE_EXTENSION.equalsIgnoreCase(extension)) {
File idmFile = extractIdmFromCEPFile(tempFile);
surveyBackupInfoExtractorJob.setFile(idmFile);
} else {
surveyBackupInfoExtractorJob.setFile(tempFile);
}
surveyBackupInfoExtractorJob.setValidate(false);
jobManager.start(surveyBackupInfoExtractorJob, false);
Response response = new Response();
if (surveyBackupInfoExtractorJob.isCompleted()) {
uploadedSurveyInfo = surveyBackupInfoExtractorJob.getInfo();
response.addObject("surveyBackupInfo", uploadedSurveyInfo);
SurveySummary existingSummary = surveyManager.loadSummaryByUri(uploadedSurveyInfo.getSurveyUri());
response.addObject("importingIntoExistingSurvey", existingSummary != null);
response.addObject("existingSurveyUserGroupId", existingSummary == null ? null : existingSummary.getUserGroupId());
return response;
} else {
response.setErrorStatus();
response.setErrorMessage(surveyBackupInfoExtractorJob.getErrorMessage());
return response;
}
}
use of org.springframework.web.multipart.MultipartFile in project product-recommendation-system by MrQuJL.
the class ProductController method saveProduct.
/**
* 处理添加或修改商品的请求
* @param product 要被添加或者修改的商品
* @return
*/
@RequestMapping("/saveProduct")
public String saveProduct(MultipartFile uploadFile, HttpServletRequest request, Product product) {
// 有图片就上传图片
if (uploadFile != null && uploadFile.getOriginalFilename() != null && uploadFile.getOriginalFilename() != "") {
// 获取上传文件的名称
String fileName = uploadFile.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
// 为了保险起见,给上传的图片重新指定一个名称
String tempFileName = UUID.randomUUID().toString() + suffix;
File fileTemp = new File(IMG_SERVER_PATH);
if (!fileTemp.exists()) {
fileTemp.mkdirs();
}
File file = new File(IMG_SERVER_PATH + "/" + tempFileName);
try {
// 讲上传的文件写入指定路径
uploadFile.transferTo(file);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 先把原来在服务器的图片删掉,再为商品赋新的图片的url
System.out.println(product.getImgSrc());
// 获取原服务器上图片的存储位置
String absolutePath = product.getImgSrc().replaceAll("/images/product", IMG_SERVER_PATH);
File originPic = new File(absolutePath);
if (originPic.exists()) {
originPic.delete();
}
product.setImgSrc("/images/product/" + tempFileName);
}
if (product.getProductId() == null) {
// 添加商品
boolean flag = this.productService.saveProduct(product);
if (flag) {
System.out.println("添加商品成功");
} else {
System.out.println("添加商品失败");
}
} else {
// 修改商品
System.out.println("进入修改商品的请求");
boolean flag = this.productService.updateProduct(product);
if (flag) {
System.out.println("修改商品成功");
} else {
System.out.println("修改商品失败");
}
}
return "redirect:/sysmgr/product/gotoProductEdit/-1";
}
Aggregations