Skip to content

Instantly share code, notes, and snippets.

@zubayerahamed
Last active April 11, 2018 12:44
Show Gist options
  • Save zubayerahamed/5a4c65b50be038a3824524d999f3203a to your computer and use it in GitHub Desktop.
Save zubayerahamed/5a4c65b50be038a3824524d999f3203a to your computer and use it in GitHub Desktop.
File Upload system in Spring Boot App
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/upload/system1}" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
</form>
<form th:action="@{/upload/system2}" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
</form>
<div>
For System 1
<img th:if="${imageName}" th:src="@{/resources/} + ${imageName}"/>
<br/>
<hr/>
<br/>
For System 2
<img th:if="${imageName}" th:src="@{/resources/images/} + ${imageName}"/>
</div>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.coderslab</groupId>
<artifactId>file-upload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>file-upload</name>
<description>File Upload System</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>file-upload</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.coderslab.controller;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.coyote.UpgradeProtocol;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@RequestMapping("/upload")
public class UploadController {
@PostMapping("/system1")
public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request, RedirectAttributes redirect) throws IOException {
String uploadPath = request.getServletContext().getRealPath("resources/");
System.out.println(uploadPath);
File dir = new File(uploadPath);
if(!dir.exists()) {
dir.mkdirs();
}
String fileName = file.getOriginalFilename();
fileName = UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf("."));
Files.copy(file.getInputStream(), Paths.get(uploadPath, fileName));
System.out.println(uploadPath + File.separator + fileName);
redirect.addFlashAttribute("imageName", fileName);
return "redirect:/upload";
}
// This is another way to upload a file without HttpServletRequest
@PostMapping("/system2")
public String upload(@RequestParam("file") MultipartFile file, RedirectAttributes redirect) throws IOException {
String uploadPath = "/opt/images/";
File dir = new File(uploadPath);
if(!dir.exists()) {
dir.mkdirs();
}
//generate random file namee
String fileName = file.getOriginalFilename();
fileName = UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf("."));
Files.copy(file.getInputStream(), Paths.get(uploadPath, fileName));
//upload file on server
redirect.addFlashAttribute("imageName", fileName);
return "redirect:/upload";
}
}
package com.coderslab.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/upload").setViewName("fileupload");
}
//this is for System2 only
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/resources/images/**").addResourceLocations("file:/opt/images/");
}
}
@zubayerahamed
Copy link
Author

This is a perfect way to upload a file on Spring Boot web application.
You can upload file into same directory when the application run on both in ide and live server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment