Multipart File to file error - spring

I want to upload a multipart file to AWS S3. So, i have to convert it.
But new File method needs a local location to get the file.
I am able to do in local. But running this code in every machine seems like a issue.
Please find both scenarios.
Working
private File convertMultiPartToFile(MultipartFile multipartFile) throws IOException {
File convFile = new File("C:\\Users\\" + multipartFile.getOriginalFilename());
multipartFile.transferTo(convFile);
return convFile;
}
Not working
private File convertMultiPartToFile(MultipartFile multipartFile) throws IOException {
File convFile = new File(multipartFile.getOriginalFilename());
multipartFile.transferTo(convFile);
return convFile;
}
Error received :
java.io.FileNotFoundException: newbusiness.jpg (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)

You could use Spring Content S3. This will hide the implementation details so you don't need to worry about them.
There are Spring Boot starter alternatives but as you are not using Spring Boot add the following dependency to your pom.xml
pom.xml
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-s3</artifactId>
<version>0.0.11</version>
</dependency>
Add the following configuration that creates a SimpleStorageResourceLoader bean:
#Configuration
#EnableS3Stores
public class S3Config {
#Autowired
private Environment env;
public Region region() {
return Region.getRegion(Regions.fromName(env.getProperty("AWS_REGION")));
}
#Bean
public BasicAWSCredentials basicAWSCredentials() {
return new BasicAWSCredentials(env.getProperty("AWS_ACCESS_KEY_ID"), env.getProperty("AWS_SECRET_KEY"));
}
#Bean
public AmazonS3 client(AWSCredentials awsCredentials) {
AmazonS3Client amazonS3Client = new AmazonS3Client(awsCredentials);
amazonS3Client.setRegion(region());
return amazonS3Client;
}
#Bean
public SimpleStorageResourceLoader simpleStorageResourceLoader(AmazonS3 client) {
return new SimpleStorageResourceLoader(client);
}
}
Create a "Store":
S3Store.java
public interface S3Store extends Store<String> {
}
Autowire this store into where you need to upload resources:
#Autowired
private S3Store store;
WritableResource r = (WritableResource)store.getResource(getId());
InputStream is = // plug your input stream in here
OutputStream os = r.getOutputStream();
IOUtils.copy(is, os);
is.close();
os.close();
When your application starts it will see the dependency on spring-content-s3 and your S3Store interface and inject an implementation for you, therefore, you don't need to worry about implementing this yourself.
IF you writing some sort of web application or microservice and you need a REST API then you can also add this dependency:
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>0.0.11</version>
</dependency>
Update your S3Config.java as follows:
#Configuration
#EnableS3Stores
#Import(RestConfiguration.class)
public class S3Config {
...
Update your store as follows:
S3Store.java
#StoreRestResource(path="s3docs")
public interface S3Store extends Store<String> {
}
Now when your application starts it will see your Store interface and also inject an #Controller implementation that will forward REST request onto your store. This replaces the autowiring code above obviously.
Then:
curl -X POST /s3docs/example-doc
with a multipart/form-data request will store the image in s3.
curl /s3docs/example-doc
will fetch it again and so on. This controller supports full CRUD and video streaming by the way.
If you want to associate this "content" with JPA Entity or something like that then you can have your S3Store extend AssociateStore or ContentStore and you have additional methods available that provide for associations.
There are a couple of getting started guides here. The s3 reference guide is here. And there is a tutorial video here. The coding bit starts about 1/2 way through.
HTH

Since it needs a temporary location to place files. Below code worked after deploying war on AWS.
private File convertMultiPartToFile(MultipartFile multipartFile) throws IOException {
File convFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") +
multipartFile.getOriginalFilename());
multipartFile.transferTo(convFile);
return convFile;
}

You have problems with relative Paths
You can do this
public class UploadStackoverflow {
private String location = "upload-dir";
private Path rootLocation;
public File convertFile(MultipartFile file) throws IOException {
rootLocation = Paths.get(location);
Files.createDirectories(rootLocation);
String filename = StringUtils.cleanPath(file.getOriginalFilename());
InputStream inputStream = file.getInputStream();
Files.copy(inputStream, this.rootLocation.resolve(filename),
StandardCopyOption.REPLACE_EXISTING);
return new File(this.rootLocation.resolve(filename).toAbsolutePath().toString());
}
}

Related

How to enable Spring Boot to display a list of files under a directory

I have a folder structure /data/reports on a file system, which contains all reports.
How can I configure a SpringBoot application to serve the contents of this file sytem.
Currently I have tried few options, but none working
#Configuration
#EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
#Value(value = "${spring.resources.static-locations:#{null}}")
private String fileSystem;
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/data/reports/**")
.addResourceLocations(fileSystem)
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
and in application.properties I have defined
spring.resources.static-locations=file:///data/reports
server.servlet.jsp.init-parameters.listings=true
But in both cases, when I try
http://host:port/application/data/reports
I'm getting 404
What am I missing ?
Based on the suggestions given, I realized that one mistake I'm doing is to access the reports via
http://host:port/application/data/reports
instead of
http://host:port/data/reports
if I use application in the request, those calls will go through RequestDispatcher and will try to find for a matching RequestMapping, which does not exist. I think I'm convinced so far.
But the problem I'm seeing now is, I'm getting SocketTimeoutException while trying to read from the resource listed in the URL. I had put some breakpoints in Spring source "ResourceHttpMessageConverter.java"
protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
try {
InputStream in = resource.getInputStream(); //It is timing out here
try {
StreamUtils.copy(in, outputMessage.getBody());
}
catch (NullPointerException ex) {
// ignore, see SPR-13620
}
The resource is a small text file with 1 line "Hello World". Yet it is timing out.
The resource in the above class is a FileUrlResource opened on file:///c:/data/reports/sample.txt
On the other hand, I tried to read that resource as
File file = new File("c:/data/reports/sample.txt");
System.out.println(file.exists());
URL url = file.toURI().toURL();
URLConnection con = url.openConnection();
InputStream is = con.getInputStream(); //This works
Thanks

Images not getting saved under resources directory spring boot mvc application

I am trying to save image files under a directory that resides in project's resources folder, below code doesn't give any error neither it saves file to the mentioned location, i know saving file to the resources is a bad idea but just giving it a try will move to upload file to S3 bucket later on.
#PostMapping("/add")
public String addBook(#ModelAttribute("book") Book book, HttpServletRequest request) throws IOException {
bookService.save(book);
MultipartFile bookImage = book.getBookImage();
try {
byte[] bytes = bookImage.getBytes();
String bookName = book.getId() + ".png";
BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream(new File(
"src/main/resources/static/image/books/"+bookName
)));
bf.write(bytes);
bf.close();
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:bookList";
}
Based on your question and comments above. Also as you are thinking of moving from filesystem storage to S3 then you may want to take a look at community project called [Spring Content][1]. This project allows you to manage content (i.e. your generated book images) and associate them with your Spring Data Entities. It provides the same programming model as Spring Data, just for unstructured content like files, images, videos etc.
For example, assuming you are using Spring Data, you could add this to your projects as follows.
pom.xml (for Spring Web MVC. Spring Boot Starters also available)
<!-- Spring dependencies -->
...
<!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs</artifactId>
<version>1.0.0.M11</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>1.0.0.M11</version>
</dependency>
StoreConfig.java
#Configuration
#EnableFilesystemStores
#Import(RestConfiguration.class)
public class EnableFilesystemStoresConfig {
#Bean
File filesystemRoot() throws IOException {
return new File("/path/to/your/book/images");
}
#Bean
FileSystemResourceLoader fileSystemResourceLoader() {
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
}
BookImageStore.java
#StoreRestResource(path="bookImages")
public interface BookImageStore extends ContentStore<Book, String> {
}
And to add Spring Content-annotated fields to your Spring Data entities, like this:
Book.java
#Entity
public class Book {
#Id
#GeneratedValue
private long id;
...other existing fields...
#ContentId
private String contentId;
#ContentLength
private long contentLength = 0L;
#MimeType
private String mimeType;
...
}
This is all you need. When your application starts it will see the `spring-content-fs` dependency on your classpath and the BookImageStore interface and it will inject a file-based implementation. Moreover, it will see the spring-content-rest dependency and inject an #Controller implementation providing a REST API for handling your book images that will forward REST calls onto the BookStoreImage so you dont have to worry about implementing any of this yourself.
So:
`POST /bookImages/{bookId} -F "image=#/some/path/to/an/image.jpg"`
will upload `image.jpg` to `/path/to/your/uploaded/images/` and update the fields on the Book entity so the content is associated.
`GET /bookImages/{bookId}` -H 'Accept: image/jpeg'
will fetch it again.
A couple of additional notes;
- I would strongly recommend that you use a path outside of src/resources so that have more flexibility with how you deploy your war/jar
- if later on you want to change the backend storage from the filesystem to s3 all you would have to do it switch out the `spring-content-fs` dependency for `spring-content-s3` and update the StoreConfig to provide an S3 client bean instead of a FilesystemResourceLoader bean.
HTH
[1]: https://paulcwarren.github.io/spring-content/

How can I get an application context inside a method of a Spring RestController annotated class

I have a simple app using the #SpringBootAnnotation with a single call on the main method:
SpringApplication.run(App.class, args);
On App.java I am also defining a couple of #BeanS, which give the instance of drivers to access external services:
#Bean
public APEWebservice ape() {
return new APEWebservice(apeWebAddress + ":" + apePort);
}
Then, on the method of one of my #RestControllerS I want to make an access to these beans, so that I can make further calls to these other services, something along these lines:
#PostMapping(path="/talk", consumes = "application/json")
#ResponseStatus(HttpStatus.ACCEPTED)
public Talk talk(#RequestBody InputTalk body) throws ConfigurationException {
ApplicationContext context = new AnnotationConfigApplicationContext(App.class);
APEWebservice ape = context.getBean("ape", APEWebservice.class);
String DRSString = ape.getSoloOutput(input, OutputType.DRSXML);
((ConfigurableApplicationContext)context).close();
try {
Commanded transformed = Preprocessor.transform(body.getContent(), DRSString);
return new Talk(counter.incrementAndGet(), transformed.execute());
}
catch (WrongCommandException e) {
return new Talk(counter.incrementAndGet(), e.getError());
}
}
This looks very ugly, and I am certain I am completely missing the point of Spring and dependency injection. Is there a way to access the context without having to initialize it for every call to the API?
I am using SpringBoot 2.2.1
What about injecting?
#Autowired
private final APEWebservice service;
//code
#PostMapping(path="/talk", consumes = "application/json")
#ResponseStatus(HttpStatus.ACCEPTED)
public Talk talk(#RequestBody InputTalk body) throws ConfigurationException {
String DRSString = service.getSoloOutput(input, OutputType.DRSXML);

Olingo with Spring Boot

I am using this tutorial and it works for a simple java web application. Now I want to convert it to Spring Boot. I remove the web.xml and add the following two annotations to DemoServlet
#RestController
public class DemoServlet extends DispatcherServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(DemoServlet.class);
#RequestMapping("/DemoService.svc/*")
protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
// create odata handler and configure it with CsdlEdmProvider and Processor
OData odata = OData.newInstance();
ServiceMetadata edm = odata.createServiceMetadata(new DemoEdmProvider(), new ArrayList<EdmxReference>());
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new DemoEntityCollectionProcessor());
// let the handler do the work
handler.process(req, resp);
} catch (RuntimeException e) {
LOG.error("Server Error occurred in ExampleServlet", e);
throw new ServletException(e);
}
}
}
I also change the HTTPServlet to DispatcherServlet.
Now I am only able to access one end point. i.e.
http://localhost:8080/DemoService.svc/
The metadata end point is not working. It returns the service document instead of xml content.
http://localhost:8080/DemoService.svc/$metadata
Can somebody explain what is going on here?
user the below code for the process method.
handler.process(new HttpServletRequestWrapper(request) {
// Spring MVC matches the whole path as the servlet path
// Olingo wants just the prefix, ie upto /odata, so that it
// can parse the rest of it as an OData path. So we need to override
// getServletPath()
#Override
public String getServletPath() {
return "/DemoService.svc";
}
}, response);
You can create a #Configuration and Map your servlet in it like the following
#Bean
public ServletRegistrationBean odataServlet() {
ServletRegistrationBean odataServRegstration = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(),
"/DemoService.svc/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication");
initParameters.put("org.apache.olingo.odata2.service.factory",
"com.metalop.code.samples.olingo.springbootolingo2sampleproject.utils.JPAServiceFactory");
odataServRegstration.setInitParameters(initParameters);
return odataServRegstration;
}
Add the following after the handler.register call:
req.setAttribute("requestMapping", "/DemoService.svc");
The best implementation of olingo2 and spring-boot can be found here. I would suggest to take a look at this repository, it is very straight forward and easy.

Spring Boot Security Scans on File Upload

I am adding a POST endpoint to a Spring Boot REST service to allow it to accept file uploads from web apps and other sources:
#PostMapping("/fileUpload")
public ResponseEntity uploadFile(#RequestParam("file") MultipartFile file) {
// ...
}
I'd like to run a security check against this file and see if there's anything malicious inside of it. Does Spring have any tools or libs to help with such an effort?
Spring Security does not provide any type of scanning for malicious files. You will need to use an anti virus tool that provides a Java API that you can use in your application. Off the top of my head I know Symantec offers a Java API, have a look here.
https://www.symantec.com/connect/articles/how-use-symantec-scan-engine-52-content-scanning-technologies-direct-integration-your-appli
Add the ClamAV Java library (ClamAV4J) as a dependency in your project's build file.
Create a service class that uses the ClamAV4J library to scan files for viruses.
#Service
public class VirusScanService {
private final ClamAVClient client;
public VirusScanService(ClamAVClient client) {
this.client = client;
}
public boolean isFileInfected(MultipartFile file) throws IOException {
return client.scan(file.getInputStream()).isInfected();
}
}
#RestController
public class FileUploadController {
private final VirusScanService virusScanService;
public FileUploadController(VirusScanService virusScanService) {
this.virusScanService = virusScanService;
}
#PostMapping("/upload")
public String handleFileUpload(#RequestParam("file") MultipartFile file) throws IOException {
if (virusScanService.isFileInfected(file)) {
return "The file is infected!";
} else {
// save the file and return a success message
return "File uploaded successfully!";
}
}
}

Resources