Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Kod źródłowy do wpisu o wysyłaniu plików na serwer za pomocą spring boota i javy znajdujący się pod adresem:
<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>File upload</title>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload"/>
<button type="submit">Wyślij</button>
public class UploadController {
@ResponseBody // 1
public String handleFile(@RequestPart(name = "fileupload") MultipartFile file) { // 2
File uploadDirectory = new File("uploads");
uploadDirectory.mkdirs(); // 3
File oFile = new File("uploads/" + file.getOriginalFilename());
try (OutputStream os = new FileOutputStream(oFile);
InputStream inputStream = file.getInputStream()) {
IOUtils.copy(inputStream, os); // 4
} catch (IOException e) {
return "Wystąpił błąd podczas przesyłania pliku: " + e.getMessage();
return "ok!";
return "ok!";
public ResponseEntity showImage(@PathVariable String name) throws IOException {
File file = new File("uploads/" + name);
if (!file.exists()) {
return ResponseEntity.notFound().build();
return ResponseEntity.ok()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment