Spring Boot Generic File Servicehttps://www.springboot.ch/blog/springboot/genericfileservice?&atomFri, 29 Mar 2024 22:23:39 +0000stack.ch
https://stack.ch/
fe6a447d-ee1a-11ee-8c11-005056bb85fbSimtech AG - Blog - Spring Boot Blogs - Spring Boot Generic File Service
https://www.springboot.ch/blog/springboot/genericfileservice
fe6a456d-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +0000Spring Boot Generic File Service
https://www.springboot.ch/blog/springboot/genericfileservice
fe6a4694-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +0000
https://www.springboot.ch/blog/springboot/genericfileservice
fe6a478a-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +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/blog/springboot/genericfileservice
fe6a48e7-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +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/blog/springboot/genericfileservice
fe6a4e96-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +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/blog/springboot/genericfileservice
fe6a4fee-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +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/blog/springboot/genericfileservice
fe6a5769-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +0000Client und Service sind am besten in 2 separaten Spring Boot Projekten zu programmieren z.B. mit der Eclipse IDE.
https://www.springboot.ch/blog/springboot/genericfileservice
fe6a5879-ee1a-11ee-8c11-005056bb85fbFri, 29 Mar 2024 22:23:39 +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 22:23:39 +0000fe6a5ae5-ee1a-11ee-8c11-005056bb85fbAktuell
https://www.springboot.ch/
Fri, 29 Mar 2024 22:23:39 +0000fe6a5b8f-ee1a-11ee-8c11-005056bb85fbAGB
https://www.springboot.ch/agb
Fri, 29 Mar 2024 22:23:39 +0000fe6a5c1d-ee1a-11ee-8c11-005056bb85fbBildungswege
https://www.springboot.ch/bildungswege
Fri, 29 Mar 2024 22:23:39 +0000fe6a5cae-ee1a-11ee-8c11-005056bb85fbBlog
https://www.springboot.ch/blog
Fri, 29 Mar 2024 22:23:39 +0000fe6a5d47-ee1a-11ee-8c11-005056bb85fbRufen Sie mich an
https://www.springboot.ch/callus
Fri, 29 Mar 2024 22:23:39 +0000fe6a5ddd-ee1a-11ee-8c11-005056bb85fbCharts
https://www.springboot.ch/charts
Fri, 29 Mar 2024 22:23:39 +0000fe6a5e6c-ee1a-11ee-8c11-005056bb85fbConsulting
https://www.springboot.ch/consulting
Fri, 29 Mar 2024 22:23:39 +0000fe6a5ef6-ee1a-11ee-8c11-005056bb85fbKontakt
https://www.springboot.ch/contact
Fri, 29 Mar 2024 22:23:39 +0000fe6a5f84-ee1a-11ee-8c11-005056bb85fbAusbildung/Kurse
https://www.springboot.ch/education
Fri, 29 Mar 2024 22:23:39 +0000fe6a6013-ee1a-11ee-8c11-005056bb85fbSoftware Engineering
https://www.springboot.ch/engineering
Fri, 29 Mar 2024 22:23:39 +0000fe6a60b6-ee1a-11ee-8c11-005056bb85fbFreelancer
https://www.springboot.ch/freelancer
Fri, 29 Mar 2024 22:23:39 +0000fe6a614f-ee1a-11ee-8c11-005056bb85fbImpressum
https://www.springboot.ch/impressum
Fri, 29 Mar 2024 22:23:39 +0000fe6a61df-ee1a-11ee-8c11-005056bb85fbKursleiter
https://www.springboot.ch/kursleiter
Fri, 29 Mar 2024 22:23:39 +0000fe6a6274-ee1a-11ee-8c11-005056bb85fbNetzwerk
https://www.springboot.ch/network
Fri, 29 Mar 2024 22:23:39 +0000fe6a62fd-ee1a-11ee-8c11-005056bb85fbReferenzen
https://www.springboot.ch/references
Fri, 29 Mar 2024 22:23:39 +0000fe6a638f-ee1a-11ee-8c11-005056bb85fbSitemap
https://www.springboot.ch/sitemap
Fri, 29 Mar 2024 22:23:39 +0000fe6a641b-ee1a-11ee-8c11-005056bb85fbTools
https://www.springboot.ch/tools
Fri, 29 Mar 2024 22:23:39 +0000fe6a64a4-ee1a-11ee-8c11-005056bb85fbVision
https://www.springboot.ch/vision
Fri, 29 Mar 2024 22:23:39 +0000fe6a6532-ee1a-11ee-8c11-005056bb85fb