I am using springboot to upload file.
How to detect actual file mimetype after changing file extension?
Ex. if someone change abc.exe to abc.pdf then upload.
In that case I want to get actual extesion/mimetype that is .exe so I can restrict.
public void handleFileUpload(#RequestParam("file") MultipartFile file) {
System.out.println(file.getContentType());
}
Above code returning application/pdf if I change abc.exe to abc.pdf where actual file is .exe
Related
I have a set of .gz compressed files in s3 bucket. I want to download the csv file inside the .gz file. I tried to extract the .gz file and put it into the s3Object. Now i need to extract the s3 object and download the csv file inside it using java. Please advise.This is the code I used.Now i am able to download gz file. But I need to download csv file inside gz.
S3Object object = s3Client.getObject(“bucket”,“Location/file.gz”);
final String encoding = null;
return ResponseEntity.ok(IOUtils.toString(object.getObjectContent(), encoding));
I need help in unzipping the gz file in s3object and return the decompressed contents in the response.
The below code will convert your gunzip file into plain data, but I'm not sure 100% about your actual issue, whether you want to display the content in browser itself or you want to send it as Save as Option, that's why I did a minimum code change to your code assuming you have only problem in converting gunzip format to CSV data, hope you could modify/enhance it that suits you best.
import java.util.zip.GZIPInputStream;
//your method begins from here
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
S3Object object = s3.getObject("your-bucket", "file-path");
return ResponseEntity.ok(IOUtils.toString(new GZIPInputStream(object.getObjectContent())));
The following code works fine on my Eclipse IDE.
private void getLayersAndDisplay() throws Exception {
URL imageURL = ImageLab.class.getResource("earthlights.jpg");
File imageFile = new File(imageURL.toURI());
URL shapeFileURL = ImageLab.class.getResource("countries.shp");
File shapeFile = new File(shapeFileURL.toURI());
URL shapeFileURL2 = ImageLab.class.getResource("Brasil.shp");
File shapeFile2 = new File(shapeFileURL2.toURI());
displayLayers(imageFile, shapeFile,shapeFile2);
}
However, when compiling to a jar, it gives me a null pointer exception. I thought that since I am getting it as a class.getResource, it would work. Can't I use the File class in a jar? Not even in a cast?
Thank you
An entry of a zip file (that's what a jar file is) is not a file existing in your file system. So you can't use a File, which represents a path on your filesystem, to refer to a zip entry. And you can't use file IO to read its content, since it's not a file.
I have no idea what you want to do, but if you want to read the content of the jar resource, just use ImageLab.class.getResourceAsStream() to get an InputStream back, reading from the entry.
I am using MultipartFile to upload file by using multipartFile.transferTo(new java.io.File(saveDirectory,fileName));. How MultipartFile getting file path. In file upload we will get only file name.
My code is:
if (null != files && files.size() > 0) {
for (MultipartFile multipartFile : files) {
fileName = multipartFile.getOriginalFilename();
multipartFile.transferTo(new java.io.File(saveDirectory, fileName));
System.out.println("kkkkkkkkk" + fileName);
nameoffile = fileName;
fileNames.add(fileName);
//Handle file content - multipartFile.getInputStream()
}
}
import com.oreilly.servlet.MultipartRequest;
You can use MultipartRequest m=new MultipartRequest(request,"d:/new");
MultipartFile.getOriginalFilename may contain path information from the client filesystem depending on the browser you are using. Most browsers like Firefox and Chrome will include include filename without path information. IE at one time did include the complete path information but not sure if that's still true with newer versions of IE.
For more information, see the MultipartFile reference documentation.
I'm using Play Framework 2.0.2 to create an application that modifies Excel files uploaded by the user. Once the Excel file is uploaded and modified (server-side), the file is automatically downloaded by the user's browser.
However, using this code:
public static Result upload() throws IOException {
Http.MultipartFormData body = request().body().asMultipartFormData();
Http.MultipartFormData.FilePart filePart = body.getFile("uploadedFile");
modifyExcelFile(filepart.getFile()); // this method modifies the uploaded Excel file, and copies it to a file named "copy.xlsx"
return ok(new File("copy.xlsx"));
}
the file that is downloaded by the client will be named after the current Controller. For example, if my Controller is named UploadController, the downloaded file is surprisingly named uploadcontroller.xlsx.
Any idea on how I could modify my code in order to have a tighter control on the downloaded file's name? I would like the downloaded file to be named copy.xlsx, not uploadcontroller.xlsx.
Just add this in the response header:
response().setHeader("Content-Disposition", "attachment; filename=FILENAME");
Where FILENAME is the name you want your file to have.
Using Tomcat and Struts 2.
public FileAction class
{
......
public upload()
{
.....
String fullFileName = request.getContextPath() + "/productImages/" + filename;
File theFile = new File(fullFileName);
FileUtils.copyFile(upload, theFile);
.....
}
}
The problem is when I upload an image it will not add image in localhost:8085/shoppingCart/productImages and also not give any exception.
But when i write String fullFileName="c:upload/productImages/" + filename; it will save the file at c:upload/productImages/ path
mean working normal in c:upload/productImages/ case
If you want the file uploaded to the server's context, you need to use ServletContext.getRealPath(...) to find the base directory of the deployed application. Note that this will not work if you're deploying a war file. Uploaded files should go to an absolute location on the server itself.
For downloading an uploaded image you'd write a stream result (since you're using Struts 2) and use it to load and stream the uploaded file back to the client.