I have written a servlet using servlet3.0 for uploading the file and it uploads the file very well. But I want to save the file in the server in the format it is uploaded by the client.
Part filePart = request.getPart("chosenFile");
String filename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
System.out.println(filePart.getContentType().split("/")[1]);
InputStream inputStream =null;
OutputStream outputStream =null;
File fileSaveDirectory = new File(UPLOAD_DIR);
if(!fileSaveDirectory.exists()){
fileSaveDirectory.mkdir();
}
String content_path = UPLOAD_DIR+File.separator+filename;//earlier
//here I was appending the string ".pdf" to every file
//but now I want the file type to be the uploaded file type.
//say if user uploads in .jpeg or any other.
System.out.println("Content Path : "+content_path);
outputStream = new FileOutputStream(new File(content_path));
inputStream = filePart.getInputStream();
int read=0;
while((read=inputStream.read())!=-1){
outputStream.write(read);
}
if(outputStream!=null)
outputStream.close();
if(inputStream !=null)
inputStream.close();
How to keep the file type , the type of uploaded file. Please help !!!