作用:將全部上傳文件保存到指定目錄下,並返回保存的文件個數。
原型:public int save(String destPathName)
和public int save(String destPathName,int option)
其中,destPathName爲文件保存目錄,option爲保存選項,它有三個值,分別是SAVE_PHYSICAL,SAVE_VIRTUAL和SAVE_AUTO。(同File類的saveAs方法的選項之值類似)SAVE_PHYSICAL指示組件將文件保存到以操作系統根目錄爲文件根目錄的目錄下,SAVE_VIRTUAL指示組件將文件保存到以Web應用程序根目錄爲文件根目錄的目錄下,而SAVE_AUTO則表示由組件自動選擇。
注:save(destPathName)作用等同於save(destPathName,SAVE_AUTO)。
<form method="post" action="uploadfile.jsp" enctype="multipart/form-data">
<input type="file" name="file" size="50">
</form>
這裏enctype="multipart/form-data"是form的MIME編碼,這個參數纔可以上傳或下載文件。
<% mySmartUpload.initialize(pageContext); //執行初始化操作
mySmartUpload.upload(); //upload file data
int size = 1024 * 1024 * 1024;
if (mySmartUpload.getFiles().getSize() > size) {
out.println("the files have to be < 1024MB !");
} else {
try {
mySmartUpload.save("/Upload");
out.print("成功上傳文件! ");
} catch (Exception e) {
out.print(e.toString());
}
}%>
這裏通過save()方法,將文件上傳到根目錄的Upload文件夾中。
1、saveAs作用:將文件換名另存。
原型:
public void saveAs(java.lang.String destFilePathName)
或
public void saveAs(java.lang.String destFilePathName, int optionSaveAs)
其中,destFilePathName是另存的文件名,optionSaveAs是另存的選項,該選項有三個值,分別是SAVEAS_PHYSICAL,SAVEAS_VIRTUAL,SAVEAS_AUTO。SAVEAS_PHYSICAL表明以操作系統的根目錄爲文件根目錄另存文件,SAVEAS_VIRTUAL表明以Web應用程序的根目錄爲文件根目錄另存文件,SAVEAS_AUTO則表示讓組件決定,當Web應用程序的根目錄存在另存文件的目錄時,它會選擇SAVEAS_VIRTUAL,否則會選擇SAVEAS_PHYSICAL。
例如,saveAs("/upload/sample.zip",SAVEAS_PHYSICAL)執行後若Web服務器安裝在C盤,則另存的文件名實際是c:\upload\sample.zip。而saveAs("/upload/sample.zip",SAVEAS_VIRTUAL)執行後若Web應用程序的根目錄是webapps/jspsmartupload,則另存的文件名實際是webapps/jspsmartupload/upload/sample.zip。saveAs("/upload/sample.zip",SAVEAS_AUTO)執行時若Web應用程序根目錄下存在upload目錄,則其效果同saveAs("/upload/sample.zip",SAVEAS_VIRTUAL),否則同saveAs("/upload/sample.zip",SAVEAS_PHYSICAL)。
建議:對於Web程序的開發來說,最好使用SAVEAS_VIRTUAL,以便移植。
SAVEAS_PHYSICAL 是絕對路徑,SAVEAS_VIRTUAL是相對路徑(相當於前面加上Tomcat/Webapps/YourProject/)。
<%
mySmartUpload.initialize(pageContext); //initiate
mySmartUpload.upload(); //upload file data
int size = 1024 * 1024 * 1024;
if (mySmartUpload.getFiles().getSize() > size) {//control the size of the file
out.println("the files have to be < 1024MB !");
} else {
try {
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the files
File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing())
continue;
String virtualPath = "/Upload/";//Tomcat/webapps/YourProject/Upload
file.saveAs(virtualPath + file.getFileName(),
mySmartUpload.SAVE_VIRTUAL);
}
out.print("成功上傳文件! ");
} catch (Exception e) {
out.print(e.toString());
}
}
%>
上述代碼使用了SaveAs方法,其中SAVEAS_VIRTUAL,存放到Web項目中的,Upload文件夾中。
下面的代碼使用了SAVEAS_PHYSICAL,和上面的代碼相同功能,其中 pageContext.getServletContext().getRealPath("/")來獲得Webapps/Project的路徑。
<%
mySmartUpload.initialize(pageContext); //initiate
mySmartUpload.upload(); //upload file data
int size = 1024 * 1024 * 1024;
if (mySmartUpload.getFiles().getSize() > size) {//control the size of the file
out.println("the files have to be < 1024MB !");
} else {
try {
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {//iterating the files
File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing())
continue;
String physicalPath = pageContext.getServletContext()//Tomcat/webapps/YourProject/Upload
.getRealPath("/") + "/Upload/";
file.saveAs(physicalPath + file.getFileName(),
mySmartUpload.SAVE_PHYSICAL);
}
out.print("成功上傳文件! ");
} catch (Exception e) {
out.print(e.toString());
}
}
%>