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

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

Introduction

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

Prototype

MultipartFile

Source Link

Usage

From source file:com.alehuo.wepas2016projekti.test.FormDomainTest.java

/**
 *
 *///w  ww  . ja v a 2  s. co m
@Test
public void parametrienAsetusToimii() {
    ImageUploadFormData fd = new ImageUploadFormData();

    fd.setDescription("test");

    assertEquals("Kuvausta ei aseteta oikein", "test", fd.getDescription());

    MultipartFile mf = new MultipartFile() {
        @Override
        public String getName() {
            return "";
        }

        @Override
        public String getOriginalFilename() {
            return "";
        }

        @Override
        public String getContentType() {
            return "";
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public long getSize() {
            return 0;
        }

        @Override
        public byte[] getBytes() throws IOException {
            return new byte[8];
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return null;
        }

        @Override
        public void transferTo(File file) throws IOException, IllegalStateException {

        }
    };

    fd.setFile(mf);

    assertEquals("Tiedostoa ei aseteta oikein", mf, fd.getFile());
}

From source file:org.tangram.spring.StreamingMultipartResolver.java

@Override
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
    ServletFileUpload upload = new ServletFileUpload();
    upload.setFileSizeMax(maxUploadSize);
    String encoding = determineEncoding(request);
    Map<String, String[]> multipartParameters = new HashMap<>();
    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
    Map<String, String> multipartFileContentTypes = new HashMap<>();

    try {/*from   w  w w  .ja v a  2 s . c  o m*/
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();

            String name = item.getFieldName();
            InputStream stream = item.openStream();
            if (item.isFormField()) {
                String value = Streams.asString(stream, encoding);
                String[] curParam = multipartParameters.get(name);
                if (curParam == null) {
                    // simple form field
                    multipartParameters.put(name, new String[] { value });
                } else {
                    // array of simple form fields
                    String[] newParam = StringUtils.addStringToArray(curParam, value);
                    multipartParameters.put(name, newParam);
                }
            } else {
                try {
                    MultipartFile file = new StreamingMultipartFile(item);
                    multipartFiles.add(name, file);
                    multipartFileContentTypes.put(name, file.getContentType());
                } catch (final IOException e) {
                    LOG.warn("({})", e.getCause().getMessage(), e);
                    MultipartFile file = new MultipartFile() {

                        @Override
                        public String getName() {
                            return "";
                        }

                        @Override
                        public String getOriginalFilename() {
                            return e.getCause().getMessage();
                        }

                        @Override
                        public String getContentType() {
                            return ERROR;
                        }

                        @Override
                        public boolean isEmpty() {
                            return true;
                        }

                        @Override
                        public long getSize() {
                            return 0L;
                        }

                        @Override
                        public byte[] getBytes() throws IOException {
                            return new byte[0];
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return null;
                        }

                        @Override
                        public void transferTo(File file) throws IOException, IllegalStateException {
                            throw new UnsupportedOperationException("NYI", e);
                        }
                    };
                    multipartFiles.add(name, file);
                    multipartFileContentTypes.put(name, file.getContentType());
                } // try/catch
            } // if
        } // while
    } catch (IOException | FileUploadException e) {
        throw new MultipartException("Error uploading a file", e);
    } // try/catch

    return new DefaultMultipartHttpServletRequest(request, multipartFiles, multipartParameters,
            multipartFileContentTypes);
}

From source file:arena.action.ServletRequestState.java

public MultipartFile getArg(final String key, final String filename, final String mimeType,
        final byte content[]) {
    Object arg = this.attributesMap.get(key);
    if (arg == null) {
        return new MultipartFile() {
            public String getName() {
                return key;
            }/*  w  w w  . j a  v a2s . co  m*/

            public String getOriginalFilename() {
                return filename;
            }

            public String getContentType() {
                return mimeType;
            }

            public boolean isEmpty() {
                return (content == null || content.length == 0);
            }

            public long getSize() {
                return (content != null ? content.length : 0);
            }

            public byte[] getBytes() {
                return content;
            }

            public InputStream getInputStream() {
                return (content != null ? new ByteArrayInputStream(content) : null);
            }

            public void transferTo(File file) throws IOException, IllegalStateException {
                if (content != null) {
                    FileUtils.writeArrayToFile(content, file);
                }
            }
        };
    } else if (arg instanceof MultipartFile) {
        return ((MultipartFile) arg);
    } else {
        throw new IllegalArgumentException("Argument " + key + " is not a multipart file");
    }
}

From source file:arena.action.ServletRequestState.java

public MultipartFile getArg(final String key, final String filename, final String mimeType,
        final InputStream content, final int length) {
    Object arg = this.attributesMap.get(key);
    if (arg == null) {
        return new MultipartFile() {
            public String getName() {
                return key;
            }/*from ww w .  j  a  v a2 s.co  m*/

            public String getOriginalFilename() {
                return filename;
            }

            public String getContentType() {
                return mimeType;
            }

            public boolean isEmpty() {
                return (length == 0);
            }

            public long getSize() {
                return length;
            }

            public InputStream getInputStream() {
                return content;
            }

            public byte[] getBytes() throws IOException {
                return FileUtils.convertStreamToByteArray(content, length);
            }

            public void transferTo(File file) throws IOException, IllegalStateException {
                if (content != null) {
                    FileUtils.writeArrayToFile(getBytes(), file);
                }
            }
        };
    } else if (arg instanceof MultipartFile) {
        return ((MultipartFile) arg);
    } else {
        throw new IllegalArgumentException("Argument " + key + " is not a multipart file");
    }
}

From source file:arena.action.ServletRequestState.java

public MultipartFile getArg(final String key, final File file) {
    Object arg = this.attributesMap.get(key);
    if (arg == null) {
        return new MultipartFile() {
            public String getName() {
                return key;
            }/*  w  ww.  j a  va2 s  .c  om*/

            public String getOriginalFilename() {
                return file.getPath();
            }

            public String getContentType() {
                return servletContext.getMimeType(FileUtils.extractFileExtension(getOriginalFilename()));
            }

            public boolean isEmpty() {
                return !file.isFile() || (file.length() == 0);
            }

            public long getSize() {
                return file.length();
            }

            public InputStream getInputStream() throws IOException {
                return new FileInputStream(file);
            }

            public byte[] getBytes() throws IOException {
                return FileUtils.convertStreamToByteArray(getInputStream(), -1);
            }

            public void transferTo(File outFile) throws IOException, IllegalStateException {
                FileUtils.copyFile(file, outFile, false);
            }
        };
    } else if (arg instanceof MultipartFile) {
        return ((MultipartFile) arg);
    } else {
        throw new IllegalArgumentException("Argument " + key + " is not a multipart file");
    }
}

From source file:de.zib.gndms.gndmc.dspace.Test.SliceClientTest.java

@Test(groups = { "sliceServiceTest" }, dependsOnMethods = { "testConfigSlice" })
public void testFileTransfer() throws IOException, NoSuchAlgorithmException, KeyManagementException {
    // TODO: test for nonexistance of sliceFile as initial constraint
    // create tmp testfile
    {//from  w ww.j  ava  2 s .c  o  m
        FileOutputStream testfile = new FileOutputStream(sliceFile);
        ByteArrayInputStream in = new ByteArrayInputStream(sliceFileContent.getBytes());
        FileCopyUtils.copy(in, testfile);
        testfile.flush();
        testfile.close();
    }

    // upload file
    {
        final ResponseEntity<Integer> responseEntity = sliceClient.setFileContent(subspaceId, sliceKindId,
                sliceId, sliceFileName, new MultipartFile() {
                    @Override
                    public String getName() {
                        return sliceFileName;
                    }

                    @Override
                    public String getOriginalFilename() {
                        // This is where he gets the content from.
                        return sliceFile;
                    }

                    @Override
                    public String getContentType() {
                        return null;
                    }

                    @Override
                    public boolean isEmpty() {
                        return false;
                    }

                    @Override
                    public long getSize() {
                        return 0;
                    }

                    @Override
                    public byte[] getBytes() throws IOException {
                        // don't need this
                        return null;
                    }

                    @Override
                    public InputStream getInputStream() throws IOException {
                        // don't need this
                        return null;
                    }

                    @Override
                    public void transferTo(File dest) throws IOException, IllegalStateException {
                        // don't need this
                    }
                }, admindn);

        Assert.assertNotNull(responseEntity);
        Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
    }

    // try to find uploaded file
    {
        if (!findFile())
            throw new IllegalStateException(
                    "Uploaded file " + sliceFileName + " could not be listed. Upload failed?");
    }

    // download file and compare with uploaded file
    {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final ResponseEntity<Integer> responseEntity = sliceClient.listFileContent(subspaceId, sliceKindId,
                sliceId, sliceFileName, new LinkedList<String>(), admindn, byteArrayOutputStream);

        Assert.assertNotNull(responseEntity);
        Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);

        InputStream stream = new FileInputStream(sliceFile);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileCopyUtils.copy(stream, out);
        Assert.assertEquals(byteArrayOutputStream.toByteArray(), out.toByteArray());
    }

    // delete uploaded file
    {
        final ResponseEntity<Integer> responseEntity = sliceClient.deleteFile(subspaceId, sliceKindId, sliceId,
                sliceFileName, admindn);

        Assert.assertNotNull(responseEntity);
        Assert.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
    }

    // try to not! find deleted file
    {
        if (findFile())
            throw new IllegalStateException("Still found deleted file " + sliceFileName);
    }
}