1、文件上傳html
上傳文件是Web開發中常常要用到的功能:例如在基於B/S的人事信息管理系統中上傳照片,在新聞發佈系統中上傳圖片等等。。。。。要實現文件上傳功能,就須要綜合利用java中的文件輸入和輸出相關的類。java
在TCP/IP中,最先出現的文件上傳機制是FTP。它是將文件由客服端發送到服務器的標準機制,可以考慮到跨平臺的文本和二進制格式文件。可是在jsp編程中不能使用FTP方法來上傳文件,這是由jsp 運行機制所決定的。web
下面是上傳文件的jsp頁面:編程
- <form action="file?file=upload" method="post" enctype="multipart/form-data">
- 請選擇你要上傳的文件:<input type="file" name="upload" siez="16"><br>
- <input type="submit" value="提交">
- </form>
對於文件上傳表單處理其中method必須爲post,也要增長類型enctype="multipart/form-data"。這樣就能夠把文件中的數據做爲流式數據上傳。固然不管是什麼文件格式,都可以。。。數組
下面是servlet 處理程序:瀏覽器
-
- String tempFileName = new String("tempFileName");
- File tempFile = new File("D:/"+tempFileName);
- FileOutputStream outputStream = new FileOutputStream(tempFile);
- InputStream fileSourcel = request.getInputStream();
- byte b[] = new byte[1000];
- int n ;
- while ((n=fileSourcel.read(b))!=-1){
- outputStream.write(b,0,n);
- }
-
- outputStream.close();
- fileSourcel.close();
-
- RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
- randomFile.readLine();
- String filePath = randomFile.readLine();
- int position = filePath.lastIndexOf('\\');
- CodeToString codeToString = new CodeToString();
- String filename = codeToString.codeString(filePath.substring(position,filePath.length()-1));
- randomFile.seek(0);
- long forthEnterPosition = 0;
- int forth = 1;
- while((n=randomFile.readByte())!=-1&&(forth<=4)){
- if(n=='\n'){
- forthEnterPosition = randomFile.getFilePointer();
- forth++;
- }
- }
-
- File fileupLoad = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file","upLoad");
- fileupLoad.mkdir();
- File saveFile = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename);
- RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
- randomFile.seek(randomFile.length());
- long endPosition = randomFile.getFilePointer();
- int j = 1;
- while((endPosition>=0)&&(j<=4)){
- endPosition--;
- randomFile.seek(endPosition);
- if(randomFile.readByte()=='\n'){
- j++;
- }
- }
-
- randomFile.seek(forthEnterPosition);
- long startPoint = randomFile.getFilePointer();
- while(startPoint<endPosition){
- randomAccessFile.write(randomFile.readByte());
- startPoint = randomFile.getFilePointer();
- }
- randomAccessFile.close();
- randomFile.close();
- tempFile.delete();
其中CodeToString()方法是一箇中文字符處理的方法。若是文件不進行編碼轉換,則上傳後的文件名將會是亂碼,接收的文件數據也會是亂碼:服務器
下面是CodeToString()源代碼:併發
- public String codeString(String str){
- String s = str;
- try {
- byte[] temp = s.getBytes("utf-8");
- s = new String(temp);
- return s ;
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- return s;
- }
- }
二:文件下載 實現文件下載的最簡單的方法就是使用超連接。假設在服務器上web目錄下地upload子目錄存在user.doc這個文檔。如:app
- <a href="http://localhost:8080/upload/user.doc">下載user.doc</a>
當單擊這個超級連接時,將會在瀏覽器中直接打開這個文檔,就像是把word軟件嵌入在瀏覽器中同樣。dom
打開文檔後就能夠實現另存爲了。固然在web上,最多見的方式是單擊連接後,出現「另存爲」對話框:
- String filename = request.getParameter("name");
-
- OutputStream outputStream = response.getOutputStream();
-
- byte b[] = new byte[600];
-
- File fileload = new File("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad",filename);
-
- response.setHeader("Content-disposition", "attachment;filename="+filename);
-
- response.setContentType("application/msword");
-
- long fileLength = fileload.length();
- String length = String.valueOf(fileLength);
- response.setHeader("Content_length", length);
-
- FileInputStream inputStream = new FileInputStream(fileload);
- int n = 0;
- while((n=inputStream.read(b))!=-1){
- outputStream.write(b,0,n);
- }
在該程序中,response對象的setContentType()用來定義服務器發送給客服端內容的MIME類型。這裏對MIME就不特別介紹了。事實上,凡是瀏覽器能處理的全部資源都有對應的MIME資源類型。在與服務器的交互中,瀏覽器就是對html、jsp等文件瀏覽器直接將其打開。對於word、excel等瀏覽器自身不能打開的文件則調用相應的方法。對於沒有標記MIME類型的文件。瀏覽器則根據其擴展名和文件內容猜想其類型。。。