Example usage for org.springframework.web.multipart MultipartFile getOriginalFilename

List of usage examples for org.springframework.web.multipart MultipartFile getOriginalFilename

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartFile getOriginalFilename.

Prototype

@Nullable
String getOriginalFilename();

Source Link

Document

Return the original filename in the client's filesystem.

Usage

From source file:egovframework.rte.tex.gds.service.impl.EgovGoodsServiceImpl.java

/**
 * ? ? ./*from w w w .j  av a2s  .  c  o  m*/
 * @param request
 * @param goodsVO 
 * @throws Exception
 */
public void updateGoods(GoodsVO goodsVO, final HttpServletRequest request) throws Exception {

    final MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

    GoodsImageVO[] imageList = new GoodsImageVO[2];
    // extract files
    final Map<String, MultipartFile> files = multiRequest.getFileMap();

    // process files
    String uploadLastPath = fileUploadProperties.getProperty("file.upload.path");

    String uploadPath = request.getSession().getServletContext().getRealPath("/") + uploadLastPath;
    File saveFolder = new File(uploadPath);

    //  ?
    boolean isDir = false;

    if (!saveFolder.exists() || saveFolder.isFile()) {
        saveFolder.mkdirs();
    }

    if (!isDir) {

        Iterator<Entry<String, MultipartFile>> itr = files.entrySet().iterator();
        MultipartFile file;
        String filePath;
        int i = 0; // goodsImage,detailImage  index 
        while (itr.hasNext()) {

            // ??  
            Entry<String, MultipartFile> entry = itr.next();
            file = entry.getValue();

            if (!"".equals(file.getOriginalFilename())) {

                String saveFileName;

                if (i == 0) {
                    saveFileName = goodsVO.getGoodsImageVO().getGoodsImageId();
                } else {
                    saveFileName = goodsVO.getDetailImageVO().getGoodsImageId();
                }

                imageList[i] = new GoodsImageVO(saveFileName, file.getOriginalFilename());
                // ? 
                filePath = uploadPath + "\\" + saveFileName;
                file.transferTo(new File(filePath));
            }
            i++;
        }
    }
    if (imageList[0] != null)
        goodsVO.setGoodsImageVO(imageList[0]);
    if (imageList[1] != null)
        goodsVO.setDetailImageVO(imageList[1]);

    goodsDAO.updateGoods(goodsVO);

}

From source file:com.zb.app.biz.service.impl.FileServiceImpl.java

public Result createFilePath(MultipartFile file, Long... id) {
    // ?/* ww  w . j av a2 s. c  o  m*/
    if (file == null) {
        return Result.failed();
    }
    if (!file.isEmpty()) {
        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            return Result.failed();
        }
        // int lastIndex = StringUtils.lastIndexOf(file.getOriginalFilename(), ".");
        // String suffix = StringUtils.substring(file.getOriginalFilename(), lastIndex);
        String[] suffixArray = StringUtils.split(file.getOriginalFilename(), ".");
        if (Argument.isEmptyArray(suffixArray)) {
            return Result.failed();
        }
        String prefix = null;
        if (Argument.isEmptyArray(id)) {
            prefix = SerialNumGenerator.createSerNo(null, SerialNumGenerator.p_prefix);
        } else {
            prefix = SerialNumGenerator.createSerNo(id[0], SerialNumGenerator.p_prefix);
        }
        String suffix = null;
        if (suffixArray.length == 1) {
            suffix = "jpg";
        } else {
            suffix = suffixArray[suffixArray.length - 1];
        }
        String filePath = prefix + DELIMITER + suffix;
        try {
            // 
            file.transferTo(new File(UPLOAD_TMP_PATH + filePath));
            return Result.success(null, STATIC_TMP_IMG + filePath);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return Result.failed();
        }
    }
    return Result.failed();
}

From source file:com.climate.oada.controller.OADAAPIController.java

@Override
public Map<String, String> uploadFile(
        @RequestHeader(value = "Authorization", required = true) String accessToken,
        @RequestParam("file") MultipartFile file) {
    Map<String, String> retval = new HashMap<String, String>();
    try {/*from w ww  .  j ava2  s.  com*/
        boolean uploadStatus = getS3DAO().saveFile(file);
        if (uploadStatus) {
            retval.put("Uploaded file name", file.getOriginalFilename());
            retval.put("fileSize", new Long(file.getSize()).toString());
        }
    } catch (Exception e) {
        retval.put("Upload failed, reason", e.getMessage());
    }
    return retval;
}

From source file:com.goodhuddle.huddle.service.impl.ThemeServiceImpl.java

@Override
@Transactional(readOnly = false)/*from   w  ww .  j av  a 2 s . c o m*/
public Theme createThemeFromUpload(MultipartFile themeBundle, boolean activate) throws ThemeBundleException {
    try {
        log.info("Importing theme from zip file: {}", themeBundle.getOriginalFilename());

        String tempFileId = fileStore.createTempFile(themeBundle.getBytes());
        Theme theme = createThemeFromFile(fileStore.getTempFile(tempFileId), activate);
        fileStore.deleteTempFile(tempFileId);
        return theme;

    } catch (IOException e) {
        throw new ThemeBundleException("Error creating theme from bundle: " + themeBundle, e);
    }
}

From source file:com.baifendian.swordfish.webserver.service.ResourceService.java

/**
 *  hdfs //ww w  . java 2  s . co m
 *
 * @param project
 * @param name
 * @param file
 */
private boolean upload(Project project, String name, MultipartFile file) {
    // ?
    String fileSuffix = CommonUtil.fileSuffix(file.getOriginalFilename()); // file suffix
    String nameSuffix = CommonUtil.fileSuffix(name);

    // ?
    if (!StringUtils.equals(fileSuffix, nameSuffix)) {
        return false;
    }

    String localFilename = BaseConfig.getLocalResourceFilename(project.getId(), UUID.randomUUID().toString()); // ???

    fileSystemStorageService.store(file, localFilename);

    // ? hdfs ?
    String hdfsFilename = BaseConfig.getHdfsResourcesFilename(project.getId(), name);
    HdfsClient.getInstance().copyLocalToHdfs(localFilename, hdfsFilename, true, true);

    return true;
}

From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java

public Result createFilePath(MultipartFile file, IFileCreate... ihandle) {
    // ?//from www .j  a v  a2  s.  co m
    if (file == null) {
        return Result.failed();
    }
    if (!file.isEmpty()) {
        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            return Result.failed();
        }
        String[] suffixArray = StringUtils.split(file.getOriginalFilename(), DELIMITER);
        if (Argument.isEmptyArray(suffixArray)) {
            return Result.failed();
        }
        String prefix = null;
        if (Argument.isEmptyArray(ihandle)) {
            prefix = Identities.uuid2();
        } else {
            prefix = ihandle[0].build(Identities.uuid2(), StringUtils.EMPTY);
        }
        String suffix = null;
        if (suffixArray.length == 1) {
            suffix = "jpg";
        } else {
            suffix = suffixArray[1];
        }
        String filePath = prefix + DELIMITER + suffix;
        // String filePath48 = prefix + "=48x48" + DELIMITER + suffix;
        try {
            File targetFile = new File(UPLOAD_TMP_PATH + filePath);
            if (targetFile != null && targetFile.getParentFile() != null
                    && !targetFile.getParentFile().exists()) {
                logger.error("?,!");
                targetFile.getParentFile().mkdirs();
            }
            // 
            file.transferTo(new File(UPLOAD_TMP_PATH + filePath));
            // FileUtils.copyFile(new File(UPLOAD_TMP_PATH + filePath), new
            // File(UPLOAD_TMP_PATH + filePath48));
            return Result.success(null, STATIC_TMP_IMG + filePath);
        } catch (Exception e) {
            logger.error(":" + e.getMessage());
            return Result.failed(":" + e.getMessage());
        }
    }
    return Result.failed();
}

From source file:com.mmj.app.biz.service.impl.FileServiceImpl.java

public Result createFilePath(MultipartFile file, IFileCreate... ihandle) {
    // ?/*from  w w  w .  j av  a 2 s. c om*/
    if (file == null) {
        return Result.failed();
    }
    if (!file.isEmpty()) {
        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            return Result.failed();
        }
        String[] suffixArray = StringUtils.split(file.getOriginalFilename(), ".");
        if (Argument.isEmptyArray(suffixArray)) {
            return Result.failed();
        }
        String prefix = null;
        if (Argument.isEmptyArray(ihandle)) {
            prefix = SerialNumGenerator.Random30String();
        } else {
            prefix = ihandle[0].build(SerialNumGenerator.Random30String(), "");
        }
        String suffix = null;
        if (suffixArray.length == 1) {
            suffix = "jpg";
        } else {
            suffix = suffixArray[1];
        }
        String filePath = prefix + DELIMITER + suffix;
        // String filePath48 = prefix + "=48x48" + DELIMITER + suffix;
        try {
            // 
            file.transferTo(new File(UPLOAD_TMP_PATH + filePath));
            // FileUtils.copyFile(new File(UPLOAD_TMP_PATH + filePath), new File(UPLOAD_TMP_PATH + filePath48));
            return Result.success(null, STATIC_TMP_IMG + filePath);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return Result.failed();
        }
    }
    return Result.failed();
}

From source file:org.openmrs.web.controller.observation.ObsFormController.java

/**
 * Sets the value of a complex obs from an http request.
 *
 * @param obs the complex obs whose value to set.
 * @param request the http request.//from ww w  . j  a va2 s  .  co  m
 * @return the complex data input stream.
 */
private InputStream setComplexData(Obs obs, HttpServletRequest request) throws IOException {
    InputStream complexDataInputStream = null;

    if (request instanceof MultipartHttpServletRequest) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile complexDataFile = multipartRequest.getFile("complexDataFile");
        if (complexDataFile != null && !complexDataFile.isEmpty()) {
            complexDataInputStream = complexDataFile.getInputStream();

            ComplexData complexData = new ComplexData(complexDataFile.getOriginalFilename(),
                    complexDataInputStream);

            obs.setComplexData(complexData);
        }
    }

    return complexDataInputStream;
}

From source file:org.jhk.pulsing.web.controller.UserController.java

@RequestMapping(value = "/createUser", method = RequestMethod.POST, consumes = {
        MediaType.MULTIPART_FORM_DATA_VALUE })
public @ResponseBody Result<User> createUser(@RequestParam User user,
        @RequestParam(name = "picture", required = false) MultipartFile mPicture) {
    _LOGGER.debug("UserController.createUser: " + user + "; "
            + (mPicture != null ? ("picture size is: " + mPicture.getSize()) : "picture not submitted"));

    if (mPicture != null) {
        try {/*from   ww w  . j a  va2s  . co m*/
            ByteBuffer pBuffer = ByteBuffer.wrap(mPicture.getBytes());

            Picture picture = Picture.newBuilder().build();
            picture.setContent(pBuffer);
            picture.setName(mPicture.getOriginalFilename());
            user.setPicture(picture);
        } catch (IOException iException) {
            _LOGGER.error("Could not get picture bytes", iException);
        }
    }

    return userService.createUser(user);
}

From source file:com.nts.alphamaleWeb.controller.ServiceController.java

@RequestMapping(value = "/installApp", method = RequestMethod.POST)
@ResponseBody//from   ww  w .  j  av  a  2s .  c  o m
public String installApp(@RequestParam String devJson, @RequestPart("file_apk") MultipartFile mpf) {

    ResultBody<String> result = new ResultBody<String>();
    List<String> devices = getDeivceListFromString(devJson);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String path = mpf.getOriginalFilename();
    try {
        if (mpf.isEmpty()) {
            result.setCode(Code.F400);
        } else {
            File file = new File(path);
            mpf.transferTo(new File(file.getAbsolutePath()));
            String results = alphamaleController.installApp(devices, true, file.getAbsolutePath());
            String resultJson = gson.toJson(results);
            result.setCode(Code.OK, resultJson);
        }
    } catch (IOException e) {
        e.printStackTrace();
        result.setCode(Code.F400);
    }

    return result.toJson();
}