資源簡介
深入體驗Java+Web開發內幕-高級特性.pdf 高級的 張孝祥 的第二本好書深入體驗Java+Web開發內幕-高級特性.pdf 高級的 張孝祥 的第二本好書源碼遠嗎源碼源碼

代碼片段和文件信息
import?java.io.*;
import?javax.servlet.*;
import?javax.servlet.http.*;
import?org.apache.commons.fileupload.*;
import?java.util.*;
public?class?UploadServlet?extends?HttpServlet
{
public?void?doPost(HttpServletRequest?request
HttpServletResponse?response)?throws?ServletExceptionIOException
{
response.setContentType(“text/html;charset=gb2312“);
PrintWriter?out?=?response.getWriter();
??? ????
//設置保存上傳文件的目錄
String?uploadDir?=?getServletContext().getRealPath(“/upload“);
if?(uploadDir?==?null)
{
out.println(“無法訪問存儲目錄!“);
return;
}
File?fUploadDir?=?new?File(uploadDir);
if(!fUploadDir.exists())
{
if(!fUploadDir.mkdir())
{
out.println(“無法創建存儲目錄!“);
return;
}
}
????
if?(!DiskFileUpload.isMultipartContent(request))?
{
out.println(“只能處理multipart/form-data類型的數據!“);
return?;
}
????
DiskFileUpload?fu?=?new?DiskFileUpload();
//最多上傳200M數據
fu.setSizeMax(1024?*?1024?*?200);
//超過1M的字段數據采用臨時文件緩存
fu.setSizeThreshold(1024?*?1024);
//采用默認的臨時文件存儲位置
//fu.setRepositoryPath(...);
//設置上傳的普通字段的名稱和文件字段的文件名所采用的字符集編碼
fu.setHeaderEncoding(“gb2312“);
//得到所有表單字段對象的集合
List?fileItems?=?null;
try
{
fileItems?=?fu.parseRequest(request);
}
catch?(FileUploadException?e)?
{
out.println(“解析數據時出現如下問題:“);
e.printStackTrace(out);
return;
}
//處理每個表單字段
Iterator?i?=?fileItems.iterator();
while?(i.hasNext())?
{
FileItem?fi?=?(FileItem)?i.next();
if?(fi.isFormField())?
{
String?content?=?fi.getString(“GB2312“);
String?fieldName?=?fi.getFieldName();
request.setAttribute(fieldNamecontent);????
}
else
{
try?
{
String?pathSrc?=?fi.getName();
/*如果用戶沒有在FORM表單的文件字段中選擇任何文件,
那么忽略對該字段項的處理*/
if(pathSrc.trim().equals(““))
{
continue;
}
int?start?=?pathSrc.lastIndexOf(‘\\‘);
String?fileName?=?pathSrc.substring(start?+?1);
File?pathDest?=?new?File(uploadDir?fileName);????
fi.write(pathDest);
String?fieldName?=?fi.getFieldName();
request.setAttribute(fieldName?fileName);
}
catch?(Exception?e)?
{
???? out.println(“存儲文件時出現如下問題:“);
???? e.printStackTrace(out);
???? return;
}
finally??//總是立即刪除保存表單字段內容的臨時文件
{
fi.delete();
}
}
}
//顯示處理結果
out.println(“用戶:“?+?request.getAttribute(“author“)?+?“
“);
out.println(“來自:“?+?request.getAttribute(“company“)?+?“
“);
/*將上傳的文件名組合成“file1file2”這種形式顯示出來,如果沒有上傳
?*任何文件,則顯示“無”,如果只上傳了第二個文件,顯示為“file2”。*/
StringBuffer?filelist?=?new?StringBuffer();
String?file1?=?(String)request.getAttribute(“file1“);
makeUpList(filelistfile1);
String?file2?=?(String)request.getAttribute(“file2“);
makeUpList(filelistfile2);
out.println(“成功上傳的文件:“?+?
(filelist.length()==0???“無“?:?filelist.toString()));
}
/**
?*將一段字符串追加到一個結果字符串中。如果結果字符串的初始內容不為空,
?*在追加當前這段字
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????223??2007-10-15?13:19??JavaWeb高級特性書中源代碼\ch1?文件上傳組件\例程1-1\compile.bat
?????文件????????519??2007-10-15?13:19??JavaWeb高級特性書中源代碼\ch1?文件上傳組件\例程1-2\FileUpload.html
?????文件???????3829??2007-10-15?13:19??JavaWeb高級特性書中源代碼\ch1?文件上傳組件\例程1-3\UploadServlet.java
?????文件????????287??2007-10-15?13:20??JavaWeb高級特性書中源代碼\ch1?文件上傳組件\例程1-4\web.xm
?????文件????????199??2007-10-15?17:18??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-1\example1.jspx
?????文件????????147??2007-10-15?17:26??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-10\example5.jspx
?????文件????????143??2007-10-15?17:26??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-12\headInfo.tag
?????文件????????318??2007-10-15?17:27??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-13\HelloWorldSimpleTag.java
?????文件????????468??2007-10-15?17:27??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-14\jsp-taglib.tld
?????文件????????375??2007-10-15?17:28??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-15\namespaceExam.jspx
?????文件????????120??2007-10-15?17:29??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-17\example6.jspx
?????文件????????142??2007-10-15?17:29??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-18\example7.jspx
?????文件?????????57??2007-10-15?17:30??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-19\example8.jspx
?????文件????????149??2007-10-15?17:31??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-21\example9.jspx
?????文件????????137??2007-10-15?17:32??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-22\example9.jspx
?????文件????????373??2007-10-15?17:32??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-23\doctypeExam.jspx
?????文件????????105??2007-10-15?17:36??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-25\attributeExam1.jspx
?????文件?????????84??2007-10-15?17:39??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-26\attributeExam2.jspx
?????文件?????????74??2007-10-15?17:40??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-27\attributeExam3.jspx
?????文件????????324??2007-10-15?17:41??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-28\text.jspx
?????文件????????202??2007-10-15?17:42??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-29\dynaContent.jspx
?????文件????????339??2007-10-15?17:20??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-3\test.jsp
?????文件????????281??2007-10-15?17:42??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-30\headInfo.tagx
?????文件????????138??2007-10-15?17:21??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-4\web.xm
?????文件????????221??2007-10-15?17:24??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-5\example2.jspx
?????文件????????231??2007-10-15?17:23??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-6\example2.jspx
?????文件????????215??2007-10-15?17:24??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-7\example3.jspx
?????文件????????151??2007-10-15?17:25??JavaWeb高級特性書中源代碼\ch11?JSP文檔\例程11-8\example4.jspx
?????文件????????149??2007-10-15?13:20??JavaWeb高級特性書中源代碼\ch2?Servlet過濾器\例程2-1\compile.bat
?????文件???????1386??2007-10-15?13:36??JavaWeb高級特性書中源代碼\ch2?Servlet過濾器\例程2-10\LogonFilter.java
............此處省略499個文件信息
評論
共有 條評論