How to upload file from jsp?Java servlet API doesn't provide any class to handle file upload.So we have to download external library to upload file Download Jar.File uploading can be done only with doPost and by using enctype "multipart/form-data" .By default it's enctype is"application/x-www-form-urlencoded" Feature of various enctype given below
value | Description |
application/x www form urlencoded | Space of url converted to "+" symbols and special characters are converted into ASCII HEX values.All characters are encoded before sent |
mulipart/form-data | Value is required when you are using rms that have a file upload file contol(no characters are encoded) |
text/plain | Spaces are converted into "+" symbols.No specical character are encoded. |
<form name="form" method="post" enctype="multipart/form-data"action="YourController">
<input type="file" name="file" size="50" />
<input type="submit" name="Insert" value="Insert">
</form>
Coding at Controller<input type="file" name="file" size="50" />
<input type="submit" name="Insert" value="Insert">
</form>
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
if (ContextType.indexOf("multipart/form-data") >= 0) {
MultipartRequest m = new MultipartRequest(request, "yourpath");
m.getOriginalFileName("file") ;//return the name of uploaded file
}
}
MultipartRequest Constructor have if (ContextType.indexOf("multipart/form-data") >= 0) {
MultipartRequest m = new MultipartRequest(request, "yourpath");
m.getOriginalFileName("file") ;//return the name of uploaded file
}
}
MultipartRequest(HttpServletRequest request, String saveDirectory,int maxPostSize,String encoding,FileRenamePolicy policy) Source Code
request the servlet request.
- saveDirectory the directory in which to save any uploaded files.
- maxPostSize the maximum size of the POST content.
- encoding the encoding of the response, such as ISO-8859-1
- policy a pluggable file rename policy
- It automatically add number to file name
- Example sample.txt ,sample1.txt,sample2.txt and so on
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
MultipartRequest m = new MultipartRequest(request, Path, 5024 * 1024, new DefaultFileRenamePolicy());
It automatically add number to filename.MultipartRequest m = new MultipartRequest(request, Path, 5024 * 1024, new DefaultFileRenamePolicy());
But if you want to give name according to your standard then
MultipartRequest m = new MultipartRequest(request, Path,
5024 * 1024, new FileRenamePolicy() {
@Override
public File rename(File arg0) {
boolean result = arg0.renameTo(new File(arg0
.getParentFile().getAbsolutePath()
+ File.separator + "Standard"));
if (result) {
System.out.println("Renamed");
}
return arg0;
}
});
5024 * 1024, new FileRenamePolicy() {
@Override
public File rename(File arg0) {
boolean result = arg0.renameTo(new File(arg0
.getParentFile().getAbsolutePath()
+ File.separator + "Standard"));
if (result) {
System.out.println("Renamed");
}
return arg0;
}
});
But file.renameTo() have unpredictable behaviour on Windows, it seems. As the API documentation says,
Many aspects of the behavior of this method are inherently
platform-dependent: The rename operation might not be able to move a
file from one filesystem to another, it might not be atomic, and it
might not succeed if a file with the destination abstract pathname
already exists. The return value should always be checked to make sure
that the rename operation was successful.
You can use apache.commons.io library, which includes
You can use apache.commons.io library, which includes
FileUtils.moveFile()
or also the Files.move()
method in JDK 7.At bottom line we can successfully use DefaultFileRenamePolicy on windows.If you have any Confusion or any doubt then leave a message below or email me
im useing default file rename policy and it is working correctly..
bt is there any way so that i could get name with which its being saved. so that i can update the file name in data base.
how to rename file and maintain its content type, that means its extension such as .jpg etc.
From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
python Training institute in Chennai
python Training institute in Bangalore
python Training in Pune
For Blockchain training in bangalore, Visit:
Blockchain training in bangalore
Big Data and hadoop training in bangalore
Visit here -> Devops Training Bangalore
I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . All the best.
Java training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
It was wonerful reading your conent. Thankyou very much. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest
1xbet korean | legalbet.co.kr
1xbet korean.com · 1xbet korean Sports 샌즈카지노 Betting · Live Streams. All sports. All betting lines · Games. All 제왕 카지노 sports.
Least although loss perform discover early work. Matter federal room. Hospital event adult environment direction administration quite.education
Anything choose tough water team wife sometimes. Consider involve because. Concern tend lawyer realize.news headlines
However others whom community. This word affect follow.breaking news in india today
Station game bit edge. For make easy thousand goal talk painting.trending-updates