I'm doing a project of mine something like lets say youtube I've done the uploading videos part but I'm stuck on how can I playback those videos to Postman?
I've tried making the return type MultipartFile class and just returning the file but it doesn't seems to work.
#RestController
public class VideoController {
#PostMapping(value = "/upload")
public void uploadVideo(#RequestParam("video") MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
File newVideo = new File("D:\\test\\" + file.getName() + ".mp4");
FileOutputStream fos = new FileOutputStream(newVideo);
fos.write(bytes);
}
}
I don't think Postman support video streaming. Regardless, in order to stream video your VideoController would need to have a GetMapping method that supports range requests which is a non-trivial coding task.
You should take a look at the community project Spring Content. This project is an abstraction over Storage and provides a range of Storage implementations including the good old Filesystem. Importantly, it also supports video streaming out of the box as this post describes.
NB: Current version of Spring Content is 0.8.0.
Related
In my app I'm generating large pdf/csv files. I'm wondering Is there any way to stream large files in Micronaut without keeping it fully in memory before sending to a client.
You can use StreamedFile, eg:
#Get
public StreamedFile download() {
InputStream inputStream = ...
return new StreamedFile(inputStream, "large.csv");
}
Be sure to check the official documentation about file transfers.
In Java on FLEXIBLE google app engine, how do you disable caching of files? I don't care if it's disabled on the entire bucket with gsutil, or individual files when I save them, or when they're read. (I just don't want anything cached, as files are frequently replaced and use the same filename).
My code to store files:
private static Storage storageService;
public static void uploadStream(
String name, InputStream stream, String bucketName)
throws IOException, GeneralSecurityException {
Storage storage = StorageOptions.getDefaultInstance().getService();
Blob blob = storage.create(BlobInfo.newBuilder(bucketName, name).build(),stream);}
This code works flawlessly for uploading and replacing pdf files as intended. When the user views the pdf on the web page, if it was recently replaced, they see a cached copy. It takes an hour before the new version can be viewed on the web site.
I'm not sure if this is something where I need to edit the bucket, set no caching when saving the file in java, or set no caching when reading the file. My code for reading the file is:
public ByteArrayOutputStream downloadStream (String bucketName, String filePath)
throws Exception {
Storage storage = getService();
byte [] bytes = storage.readAllBytes(bucketName,filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
baos.write(bytes, 0, bytes.length);
return baos;
}
This is then returned via a web servlet.
Standard google app engine was tagged as well, as this is really a google cloud storage issue, and I'm not sure if the solution lies with gsutil or the cloud console, but note that the java code to access google cloud storage will differ between flexible and standard.
Objects are cacheable if they are publicly readable and the Cache-Control header allows caching. Thus, you can disable caching by changing either/both of these things. See this gsutil documentation about setting the Cache-Control header, for example:
https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata#cache-control
I had used this method for creating an API for downloading files using jersey JAX-RS for my REST web service :
public Response returnFile(String filePath,String fileName,MediaType type) throws IOException {
File file = new File(filePath);
String[] filePart = filePath.split("\\.");
String fileEnd = filePart[filePart.length - 1];
return Response.ok(file, type)
.header("Content-Disposition","attachment; filename=\""
+ URLEncoder.encode(fileName + "." + fileEnd,"UTF-8") + "\"" )
.build();
}
but someone told me this may have performance issues and cause OutOfMemory exception.
so I recently searched for creating an API like this and I found out most of the examples are using StreamingOutput.
so what is the advantage of using StreamingOutput? does it better to use it in my method instead of File? and if anyone says the best ways for creating these kinds of APIs (APIs for downloading files), I will be very appreciated.
Yes, it is more memory efficient. Read this article for more info: https://dzone.com/articles/jax-rs-streaming-response
I need to send images as API response.I created a response but i still cant not send the image.I am using play framework with java.
Http.Response response = new Http.Response();
response.setContentType("image/jpeg");
Thanks.
Do not really know what version of play are you using but this should work for 2.X
public static Result returnImage(){
return ok(new File("public/img/1.jpg")).as("image/jpg");
}
Here you can see that ok() can receive a File as a parameter. To check all the options you can go to Play Framework JavaDocs.
Hope it helps!
Previous answer is correct, however if image is large, I would suggest using chunks and streams:
public Result send() throws FileNotFoundException {
FileInputStream fis = new FileInputStream(new File("some_path/test.jpeg"));
response().setContentType("image/jpeg");
return ok(fis);
}
I am developing a GWT application. This application is running in a server. Well, I implement a button which calls a method that generates a local file in server side. However I would like to download/generate this file in client side. How could I do this in GWT?
Thanks
In our project we created a file on server on demand. When the file has been successful created we send notification to browser and created a link.
See servlet code:
public class DownloadServlet extends HttpServlet {
private FileManager fileManager;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String encodedFileName = req.getRequestURI().substring(
req.getContextPath().length() + req.getServletPath().length() + 1);
String decodedFileName = URLDecoder.decode(encodedFileName, "utf-8");
File downloadableFile = fileManager.toFile(decodedFileName);
ServletOutputStream os = resp.getOutputStream();
try {
InputStream is = FileUtils.openInputStream(downloadableFile);
try {
IOUtils.copy(is, os);
} finally {
is.close();
}
} finally {
os.close();
}
}
}
private native void Download(String filename, String text)/*-{
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom); }-*/;
Use JSNI method inside GWT code , provide the file name you want to download in addition to JSON string as text (String) , this method will download a file with specified content in text variable to client browser.
Current situation is, that not all browsers are able to work with local file system, so there is no universal solution in GWT. Also as far as I know FilesSstem API is not finished.
As alternative you can keep using serverside generated files, or use Flash plugin to generate and store files (you will have to create a Flash app, and create some API to control it from GWT).
You should definitely have a look at Aki Miyazaki’s HTML5 file download code for GWT.
It works on the client side as you requested.
AFAIK, as of now, it only works in Chrome, but this is supposed to change as other browsers implement the download attribute.
You can do that using Data URIs:
Make your GWT RPC method return the file content or the data to generate the file.
On the client side, format a Data URI with the file content received or generate the data content.
Use Window.open to open a file save dialog passing the formatted DataURI.
Take a look at this reference, to understand the Data URI usage:
Export to csv in jQuery