Search in sources :

Example 56 with MultipartFile

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";
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) List(java.util.List) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 57 with MultipartFile

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();
}
Also used : AjaxResult(com.hfut.entity.AjaxResult) List(java.util.List) CustomException(com.hfut.exception.CustomException) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomException(com.hfut.exception.CustomException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 58 with MultipartFile

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);
}
Also used : TransactionalCSVDataImportJob(org.openforis.collect.io.data.TransactionalCSVDataImportJob) CSVDataImportJob(org.openforis.collect.io.data.CSVDataImportJob) JobView(org.openforis.collect.web.controller.CollectJobController.JobView) CSVDataImportInput(org.openforis.collect.io.data.CSVDataImportJob.CSVDataImportInput) CollectSurvey(org.openforis.collect.model.CollectSurvey) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 59 with MultipartFile

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;
    }
}
Also used : Response(org.openforis.commons.web.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) SurveyBackupInfoExtractorJob(org.openforis.collect.io.SurveyBackupInfoExtractorJob) SurveySummary(org.openforis.collect.model.SurveySummary) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 60 with MultipartFile

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";
}
Also used : IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)272 File (java.io.File)151 IOException (java.io.IOException)71 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)71 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)53 Test (org.junit.Test)41 FileInputStream (java.io.FileInputStream)37 ArrayList (java.util.ArrayList)30 JobConfig4DB (com.vip.saturn.job.console.mybatis.entity.JobConfig4DB)28 Test (org.junit.jupiter.api.Test)27 InputStream (java.io.InputStream)25 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)24 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 FileOutputStream (java.io.FileOutputStream)18 Date (java.util.Date)18 List (java.util.List)18 PostMapping (org.springframework.web.bind.annotation.PostMapping)17 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)17 Path (java.nio.file.Path)15