由於儲存空間,對一般使用者而言,就是用來操作隱藏檔的,所以這兩天,特意看了一下windows下用servlet實現檔案上傳、下載和刪除,下面是詳細代碼說明
上傳:
用的是commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar組件,可以去apache官網上去下載,然後放到WebRoot/WEB-INF/lib目錄下
upload.html
<html><br /><head><br /> <meta http-equiv="Content-Type" content="text/html; charset=GBK"><br /> <title>File upload</title><br /></head><br /><body><br /><!-- action="fileupload"對應web.xml中<servlet-mapping>中<url-pattern>的設定.--><br /><!-- 必須是multipart的表單資料,才能完整的傳遞檔案資料 --><br /> <form name="myform" action="fileupload" method="post"<br /> enctype="multipart/form-data"><br /> File:<br><br /> <input type="file" name="myfile"><br><br /> <br><br /> <input type="submit" name="submit" value="Commit"><br /> </form><br /></body><br /></html>
web.xml裡加上下面幾行:
<servlet><br /> <servlet-name>Upload</servlet-name><br /> <servlet-class>am.demo.Upload</servlet-class><br /> </servlet><br /> <servlet-mapping><br /> <servlet-name>Upload</servlet-name><br /> <url-pattern>/fileupload</url-pattern><br /> </servlet-mapping></p><p>
src目錄下建立檔案Upload.java:
package am.demo;</p><p>import java.io.File;<br />import java.io.IOException;<br />import java.util.Iterator;<br />import java.util.List;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>import org.apache.commons.fileupload.FileItem;<br />import org.apache.commons.fileupload.disk.DiskFileItemFactory;<br />import org.apache.commons.fileupload.servlet.ServletFileUpload;</p><p>@SuppressWarnings("serial")<br />public class Upload extends HttpServlet {<br />private String uploadPath = "d://temp"; // 上傳檔案的目錄</p><p>@SuppressWarnings("unchecked")<br />public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws IOException, ServletException {<br />try {<br />// Create a factory for disk-based file items<br />DiskFileItemFactory factory = new DiskFileItemFactory();</p><p>// Create a new file upload handler<br />ServletFileUpload upload = new ServletFileUpload(factory);</p><p>List<FileItem> items = upload.parseRequest(request);// 得到所有的檔案<br />Iterator<FileItem> i = items.iterator();<br />while (i.hasNext()) {<br />FileItem fi = (FileItem) i.next();<br />String fileName = fi.getName();<br />if (fileName != null) {<br />File fullFile = new File(fi.getName());<br />File savedFile = new File(uploadPath, fullFile.getName());<br />fi.write(savedFile);<br />}<br />}</p><p>response.setContentType("text/html;charset=GBK");<br />response.getWriter().print(<br />"<mce:script language='javascript'><!--<br />alert('上傳成功');window.location.href='index.jsp';<br />// --></mce:script>");<br />} catch (Exception e) {<br />// 可以跳轉出錯頁面<br />e.printStackTrace();<br />}<br />}</p><p>public void init() throws ServletException {<br />File uploadFile = new File(uploadPath);<br />if (!uploadFile.exists()) {<br />uploadFile.mkdirs();<br />}<br />}<br />}<br />
再看下載Downlaod.java:
package am.demo;</p><p>import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.IOException;<br />import java.io.OutputStream;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>@SuppressWarnings("serial")<br />public class Download extends HttpServlet {<br />public void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws IOException, ServletException {</p><p>String aFileName = new String(request.getParameter("name").getBytes(<br />"iso8859-1"), "gbk");</p><p>File fileLoad = new File("d:/temp", aFileName);</p><p>FileInputStream in = null; // 輸入資料流<br />OutputStream out = response.getOutputStream();<br />byte b[] = new byte[1024];</p><p>try {</p><p> response.setContentType("application/x-msdownload;");</p><p>response.setHeader("Content-disposition", "attachment; filename="<br />+ new String(aFileName.getBytes("GBK"), "ISO-8859-1"));</p><p>// download the file.<br />in = new FileInputStream(fileLoad);<br />int n = 0;<br />while ((n = in.read(b)) != -1) {<br />out.write(b, 0, n);<br />}</p><p>} catch (Throwable e) {<br />e.printStackTrace();<br />} finally {<br />try {<br />in.close();<br />out.close();<br />} catch (Throwable e) {<br />e.printStackTrace();<br />}<br />}<br />}</p><p>public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws IOException, ServletException {<br />doGet(request, response);<br />}</p><p>}<br />
刪除Delete.java:
package am.demo;</p><p>import java.io.File;<br />import java.io.FileNotFoundException;<br />import java.io.IOException;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>@SuppressWarnings("serial")<br />public class Delete extends HttpServlet {</p><p>public void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws FileNotFoundException, IOException {</p><p>String aFileName = new String(request.getParameter("name").getBytes(<br />"iso8859-1"), "gbk");</p><p>File file = new File("d:/temp", aFileName);</p><p>response.setContentType("text/html;charset=GBK");</p><p>if (!file.isDirectory()) {<br />file.delete();<br />response.getWriter().print(<br />"<mce:script language='javascript'><!--<br />alert('刪除成功');window.location.href='index.jsp';<br />// --></mce:script>");<br />} else {</p><p>}</p><p>}</p><p>public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws IOException, ServletException {<br />doGet(request, response);<br />}<br />}<br />
因為ubuntu server是黑屏,本人用的是ubuntu server10.04,不方便用瀏覽器來查看檔案,因為沒有圖形介面,也不方便調試,所以可以先在windows上測試通過,然後把相應檔案拷到ubuntu server中的tomcat相應目錄下,可以通過samba來傳檔案,但遇到一個問題,就是中文亂碼的問題,因為在開始安裝時,選的是英文,後面用網上的一些辦法,還是沒有解決中文亂碼的問題,有知道的大俠,還煩請轉告