Spring Boot Generic File Servicehttps://www.springboot.ch/getParameter?&atomFri, 29 Mar 2024 11:57:53 +0000stack.ch
https://stack.ch/
932a7700-edc3-11ee-8c11-005056bb85fbSimtech AG - Blog - Spring Boot Blogs - Spring Boot Generic File Service
https://www.springboot.ch/getParameter
932a7867-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000Spring Boot Generic File Service
https://www.springboot.ch/getParameter
932a79bf-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000
https://www.springboot.ch/getParameter
932a7ac0-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000Es ist schon erstaunlich wie man auch mit Spring Boot REST Services generisch und allgemein basierend auf der Servlet Schnittstelle programmieren kann. Das folgende Programmscript zeigt den REST Controller GenericFileService mit dem wir anhand des Path URL Parameters eine lokale Datei laden und Buffered an den HTTP Client transferieren.
https://www.springboot.ch/getParameter
932a7c34-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000package ch.std.fileservice.rest;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/rest/file")
public class GenericFileService {
public GenericFileService() {
super();
}
@GetMapping
public void get(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
String path = httpServletRequest.getParameter("path");
if (path == null) {
httpServletResponse.setContentType("text/html; charset=UTF-8");
httpServletResponse.getWriter().print("path is missing");
return;
}
String mimeType = URLConnection.guessContentTypeFromName(path);
if (mimeType == null) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
mimeType = mimeTypesMap.getContentType(path);
}
httpServletResponse.setContentType(mimeType);
httpServletResponse.setHeader("Transfer-Encoding", "chunked");
OutputStream os = httpServletResponse.getOutputStream();
byte[] buffer = new byte[1 << 12];
int bytesRead = -1;
try (FileInputStream fis = new FileInputStream(path)) {
while ((bytesRead = fis.read(buffer)) > 0) {
os.write(buffer, 0, bytesRead);
os.flush();
}
} catch (Exception e) {
httpServletResponse.setContentType("text/html; charset=UTF-8");
os.write(("path " + path + " does not exist").getBytes(StandardCharsets.UTF_8));
os.flush();
}
return;
}
}
Das folgende Listing zeigt die Spring Boot Application:package ch.std.fileservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GenericFileServiceApplication {
public static void main(String[] args) {
SpringApplication.run(GenericFileServiceApplication.class, args);
}
}
https://www.springboot.ch/getParameter
932a825d-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000Nach dem Start der Spring Boot Applikation ist der File Service aktiv und kann je nach Konfiguration wie folgt adressiert werden:http://localhost:8080/rest/file?path=/mypath/myfile.txtSofern das File existiert wird es zum Client gestreamt.
https://www.springboot.ch/getParameter
932a83a8-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000Das folgende Listing zeigt den dazu passenden Spring Boot File Service Client entwickelt als Command Line Applikation:package ch.std.fileservice.client;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Arrays;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class GenericFileServiceClient implements ApplicationRunner {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(GenericFileServiceClient.class);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
URL url = null;
try {
url = new URL(args.getOptionValues("url").get(0));
} catch (Exception e) {
System.err.println("missing --url option argument");
this.help();
return;
}
String out = null;
try {
out = args.getOptionValues("out").get(0);
} catch (Exception e) {
}
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
HttpEntity entity = new HttpEntity(headers);
ResponseEntity response = restTemplate.exchange(url.toString(), HttpMethod.GET, entity, byte[].class, "1");
if (out != null) {
try (FileOutputStream fos = new FileOutputStream(out)) {
fos.write(response.getBody());
}
System.out.println("result written to file " + out);
}
private void help() {
System.out.println("usage java -jar genericfileclient-0.0.1-SNAPSHOT.jar --url= --out=");
System.out.println("example:");
System.out.println("java -jar genericfileclient-0.0.1-SNAPSHOT.jar --url=http://localhost:8080/rest/file?path=in/bigimage.jpg --out=out/bigimage.jpg");
}
}
Das File bigimage.jpg kann ersetzt werden durch eine real existierende Datei analog kann der Output Pfad angepasst werden.
https://www.springboot.ch/getParameter
932a89c9-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000Client und Service sind am besten in 2 separaten Spring Boot Projekten zu programmieren z.B. mit der Eclipse IDE.
https://www.springboot.ch/getParameter
932a8ac7-edc3-11ee-8c11-005056bb85fbFri, 29 Mar 2024 11:57:53 +0000War dieser Blog für Sie wertvoll. Wir danken für jede Anregung und FeedbackÜber uns
https://www.springboot.ch/about
Fri, 29 Mar 2024 11:57:53 +0000932a8d17-edc3-11ee-8c11-005056bb85fbAktuell
https://www.springboot.ch/
Fri, 29 Mar 2024 11:57:53 +0000932a8dbc-edc3-11ee-8c11-005056bb85fbAGB
https://www.springboot.ch/agb
Fri, 29 Mar 2024 11:57:53 +0000932a8e65-edc3-11ee-8c11-005056bb85fbBildungswege
https://www.springboot.ch/bildungswege
Fri, 29 Mar 2024 11:57:53 +0000932a8f06-edc3-11ee-8c11-005056bb85fbBlog
https://www.springboot.ch/blog
Fri, 29 Mar 2024 11:57:53 +0000932a8fa3-edc3-11ee-8c11-005056bb85fbRufen Sie mich an
https://www.springboot.ch/callus
Fri, 29 Mar 2024 11:57:53 +0000932a9043-edc3-11ee-8c11-005056bb85fbCharts
https://www.springboot.ch/charts
Fri, 29 Mar 2024 11:57:53 +0000932a90dc-edc3-11ee-8c11-005056bb85fbConsulting
https://www.springboot.ch/consulting
Fri, 29 Mar 2024 11:57:53 +0000932a917e-edc3-11ee-8c11-005056bb85fbKontakt
https://www.springboot.ch/contact
Fri, 29 Mar 2024 11:57:53 +0000932a9217-edc3-11ee-8c11-005056bb85fbAusbildung/Kurse
https://www.springboot.ch/education
Fri, 29 Mar 2024 11:57:53 +0000932a92b4-edc3-11ee-8c11-005056bb85fbSoftware Engineering
https://www.springboot.ch/engineering
Fri, 29 Mar 2024 11:57:53 +0000932a9357-edc3-11ee-8c11-005056bb85fbFreelancer
https://www.springboot.ch/freelancer
Fri, 29 Mar 2024 11:57:53 +0000932a93f1-edc3-11ee-8c11-005056bb85fbImpressum
https://www.springboot.ch/impressum
Fri, 29 Mar 2024 11:57:53 +0000932a948b-edc3-11ee-8c11-005056bb85fbKursleiter
https://www.springboot.ch/kursleiter
Fri, 29 Mar 2024 11:57:53 +0000932a9558-edc3-11ee-8c11-005056bb85fbNetzwerk
https://www.springboot.ch/network
Fri, 29 Mar 2024 11:57:53 +0000932a9606-edc3-11ee-8c11-005056bb85fbReferenzen
https://www.springboot.ch/references
Fri, 29 Mar 2024 11:57:53 +0000932a96a0-edc3-11ee-8c11-005056bb85fbSitemap
https://www.springboot.ch/sitemap
Fri, 29 Mar 2024 11:57:53 +0000932a9748-edc3-11ee-8c11-005056bb85fbTools
https://www.springboot.ch/tools
Fri, 29 Mar 2024 11:57:53 +0000932a97e9-edc3-11ee-8c11-005056bb85fbVision
https://www.springboot.ch/vision
Fri, 29 Mar 2024 11:57:53 +0000932a9881-edc3-11ee-8c11-005056bb85fb