Example usage for org.springframework.web.multipart MultipartRequest getFile

List of usage examples for org.springframework.web.multipart MultipartRequest getFile

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartRequest getFile.

Prototype

@Nullable
MultipartFile getFile(String name);

Source Link

Document

Return the contents plus description of an uploaded file in this request, or null if it does not exist.

Usage

From source file:com.mycompany.uploadfile.controller.StudentImageController.java

@RequestMapping(value = "/savefile", method = RequestMethod.POST)
//     @RequestMapping(value = "/savefile", method = RequestMethod.POST)
//    @ResponseBody
public void saveImage(MultipartRequest file) throws IOException {
    System.out/*from  w ww.ja va2  s. c  om*/
            .println("------------------------------------------------------------->" + file.getMultiFileMap());
    System.out.println("------------------------------------------------------------->" + file.getFile("file"));
    StudentImage image = new StudentImage();
    image.setContent(file.getFile("file").getBytes());
    image.setName(file.getFile("file").getOriginalFilename());
    image.setMimeType(file.getFile("file").getName());
    imageRepo.save(image);
}

From source file:com.laxser.blitz.web.paramresolver.ResolverFactoryImpl.java

@SuppressWarnings("unchecked")
private static Object resolveArray(Invocation inv, ParamMetaData metaData, Class<?> compnentType) {
    if (compnentType == MultipartFile.class) {
        String filterName = metaData.getParamName();
        if (filterName == null) {
            filterName = "";
        }/*from  w  w  w  . ja v  a  2  s .  c om*/
        if (inv.getRequest() instanceof MultipartRequest) {
            List<MultipartFile> files = new LinkedList<MultipartFile>();
            MultipartRequest multipartRequest = (MultipartRequest) inv.getRequest();
            Iterator<String> names = multipartRequest.getFileNames();
            while (names.hasNext()) {
                String name = names.next();
                if (name.startsWith(filterName)) {
                    files.add(multipartRequest.getFile(name));
                }
            }
            return files.toArray(new MultipartFile[0]);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("cann't " + "set MultipartFile param to method "
                        + ", the request is not a MultipartRequest");
            }
        }
    } else {
        Object toConvert = null;
        for (String paramName : metaData.getParamNames()) {
            if (paramName != null) {
                toConvert = inv.getRequest().getParameterValues(paramName);
                if (toConvert != null) {
                    break;
                }
            }
        }
        if (toConvert != null) {
            if (((String[]) toConvert).length == 1) {
                toConvert = ((String[]) toConvert)[0].split(",");
            }
            Class<?> arrayType;
            if (metaData.getParamType().isArray()) {
                arrayType = metaData.getParamType();
            } else {
                arrayType = arrayTypeMap.get(compnentType);
                if (arrayType == null) {
                    arrayType = Array.newInstance((Class<?>) compnentType, 0).getClass();
                }
            }
            TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
            Object array = typeConverter.convertIfNecessary(toConvert, arrayType);
            return array;
        }
    }
    return Array.newInstance((Class<?>) compnentType, 0);
}

From source file:net.risesoft.soa.asf.web.controller.SystemController.java

@RequestMapping({ "uploadLicense.do" })
@ResponseBody//from  w  w  w .  j a  v a  2 s  .c  o m
public String uploadLicense(MultipartRequest multipartRequest) {
    if (!(this.bundleHelper.isDevMode())) {
        MultipartFile multipartFile = multipartRequest.getFile("licenseFile");

        File licenseFile = new File(
                System.getProperty("user.dir") + "/../license/" + multipartFile.getOriginalFilename());
        try {
            multipartFile.transferTo(licenseFile);
            InputStream istream = null;
            try {
                istream = new FileInputStream(licenseFile);
                if (!(this.checkLicense.check(istream)))
                    throw new RuntimeException("License .");
            } finally {
                IOUtils.closeQuietly(istream);
            }
            IOUtils.closeQuietly(istream);

            File licensePath = licenseFile.getParentFile();
            if (licensePath.isDirectory()) {
                File[] files = licensePath.listFiles();
                for (File f : files) {
                    if ((!(f.isFile())) || (f.equals(licenseFile)))
                        continue;
                    f.delete();
                }
            }

            if (!(this.checkLicense.refresh()))
                throw new RuntimeException("License .");
        } catch (Exception ex) {
            log.error(" License : " + ex.getMessage(), ex);
            if (licenseFile.exists()) {
                licenseFile.delete();
            }
            return "{success:false, msg:'" + ex.getMessage() + "'}";
        }
        return "{success:true, msg:' License ?.'}";
    }
    return "{success:false, msg:' License ???.'}";
}