1. 程式人生 > >文件上傳與下載

文件上傳與下載

rest true post edate res 打印 sts stub ati

文件上傳

文件上傳的幾個步驟:

1 獲得上傳路徑 如果上傳的文件需要安全的存放 則應該放在WEB-INF 下面

  String savePath=request.getServletContext().getRealPath("/WEB-INF/upload1");

2 創建解析工廠

  DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();

3 創建解析器

  ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);

4 判斷是不是表單數據(如果是表單數據 繼續進行後面操作)

  ServletFileUpload.isMultipartContent(request)

5 使用ServletFileUpload解析器解析上傳數據,解析結果返回的是一個List<FileItem>集合,每一個FileItem對應一個Form表單的輸入項

  List<FileItem> list=servletFileUpload.parseRequest(request);

6 遍歷每一個FileItem

7 如果FileItem是普通表單項,則按普通表單項處理方式處理(取值),如果是文件則進行後面操作

  fileItem.isFormField()

8 獲取上傳文件的名字(通過截取字段獲得真實名字)

 (1) String fileName=fileItem.getName();

  此名字字符串包含包含一些系統路徑,如下面這個字符串所示。

  C:\Windows\Installer\{90140000-0011-0000-0000-0000000FF1CE}\graph.ico

(2) 截取字符串 獲得真實名字

  fileName=fileName.substring(fileName.lastIndexOf("\\")+1);

9 將fileItem寫入流中(方便後面寫到磁盤中)

  InputStream in=fileItem.getInputStream();

10 創建輸出流 並將輸入流中的數據寫入輸出流中,並刪除臨時文件

   FileOutputStream out = new FileOutputStream(sqlPath);

  

byte[] bytes=new byte[1024];
int l=0;
	while((l=in.read(bytes))!=-1){
		out.write(bytes, 0, l);
	}
							
	//刪除處理文件上傳時生成的臨時文件
	 fileItem.delete();
	msg = "文件上傳成功!";

  

文件下載的步驟:

1 獲取傳遞過來的要下載的文件的路徑和姓名參數

  String filename=req.getParameter("filename");
  String filepath=req.getParameter("filepath");

2 將要下載的文件放進輸入流中

   InputStream in = new FileInputStream(filepath);

3 設置響應頭,對文件進行url編碼

  

 filename = URLEncoder.encode(filename, "UTF-8");
 resp.setHeader("Content-Disposition", "attachment;filename="+filename); 

  

4 創建輸出流 對文件進行copy

  

  拷貝
	    OutputStream out = resp.getOutputStream();
	    byte[] b = new byte[1024];
	    int len = 0;
	    while((len = in.read(b))!=-1){
	      out.write(b, 0, len);
	    }
	    out.flush();
	    out.close();
	    in.close()

 在本實例中 我設計了一個簡單數據庫file (filename 文件名,filepath 文件路徑), 采用dao模式 將上傳的文件的路徑和文件名保存在數據庫中 ,在再前臺頁面中獎所有的已上傳的文件讀取出來顯示在頁面上 ,在使用文件下載的方式將其下載。

具體代碼如下:

《一》 文件上傳

  (1)前臺頁面

<form method="post" action="${pageContext.request.contextPath }/fileUpload1" enctype="multipart/form-data">
        上傳者:<input type="text" name="username"></br>
        <input type="file" name="file"><br>
        <input type="file" name="file1"><br>
        <input type="submit" value="submit">
    </form>

  (2) web層 (上傳成功後 跳轉到file_uplodad/g.jsp 這個頁面 註意要讓文件全部操作完後再跳轉,如果上傳的時候有個以上文件,你在上傳一個就跳轉頁面會程序報錯)

  

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		FileUploadServices fileUploadServices=new FileUploadServices();
		
		/*獲得上傳路徑*/
		String  savePath=request.getServletContext().getRealPath("/WEB-INF/upload1");
		File file=new File(savePath);
		if(!file.exists()){
			System.out.println(savePath+"不存在,請創建");
			file.mkdir();
		}
		String msg="";
		//1創建解析工廠
		DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
		//2創建解析器
		ServletFileUpload  servletFileUpload=new ServletFileUpload(diskFileItemFactory);
			//中文亂碼問題
		servletFileUpload.setHeaderEncoding("utf-8");
		//3判斷是不是表單數據
		if(!ServletFileUpload.isMultipartContent(request)){
			System.out.println("上傳的不是表單數據");
		}
		//4、使用ServletFileUpload解析器解析上傳數據,解析結果返回的是一個List<FileItem>集合,每一個FileItem對應一個Form表單的輸入項
		try {
			List<FileItem>  list=servletFileUpload.parseRequest(request);
			for(FileItem fileItem:list){
				if(fileItem.isFormField()){
					String name=fileItem.getName();
					String value=fileItem.getString("utf-8");//解決亂碼問題
					System.out.println(name+"="+value);
				}
				else{
					String fileName=fileItem.getName();//註意是getName  而不是getFileName()---file  表單中的
				if(fileName!=""){
					System.out.println(fileName+"      fileName");
					
					
					
					//C:\Windows\Installer\{90140000-0011-0000-0000-0000000FF1CE}\graph.ico
					//截取後面部分得到真實名字   \graph.ico
					fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
					System.out.println(fileName+"截取後面部分得到真實名字");
					//獲取fileItem的輸入流
					InputStream in=fileItem.getInputStream();
				
					//創建一個文件輸出流
					Date date=new Date();
					
					SimpleDateFormat SimpleDateFormat=new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
					String da=SimpleDateFormat.format(date);
					String sqlPath=savePath + "\\" +da+"_"+ fileName;
					System.out.println(sqlPath+"  sqlPath");
					//sqlPath=sqlPath.replaceAll("\\", "/");
					//判斷是否上傳了文件
					System.out.println(sqlPath.substring(sqlPath.lastIndexOf("_")+1)+"dd");
					if(sqlPath.substring(sqlPath.lastIndexOf("_")+1)!=" "){
						 FileOutputStream out = new FileOutputStream(sqlPath);
							byte[] bytes=new byte[1024];
							int l=0;
							while((l=in.read(bytes))!=-1){
								out.write(bytes, 0, l);
							}
							
		                    //刪除處理文件上傳時生成的臨時文件
		                    fileItem.delete();
		                    msg = "文件上傳成功!";
		                    
		                    String sql="insert into file (filepath,filename) values(?,?)";
		            		try {
		            			if(sqlPath.substring(sqlPath.lastIndexOf("_")+1).length()>1){
		            				sqlPath=sqlPath.replace("\\", "/");
		            				/*pst=(PreparedStatement) conn.prepareStatement(sql);
									 pst.setString(1,sqlPath );
									 pst.setString(2,fileName );
									 pst.execute();*/
		            				fileUploadServices.insert(sql,new Object[]{sqlPath,fileName});
		            			}
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
					}
					
                    
				}
			}
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		response.sendRedirect("/file_uplodad/g.jsp");	
	}

  

  (3) services層

FileUploadDao fileUploadDao =new FileUploadDao();
	public void insert(String sql,Object ...obj) {
		// TODO Auto-generated method stub
		fileUploadDao.insert(sql,obj);
		
	}

  

  (4)dao層

          Connection conn=null;
		ResultSet rs=null;
		PreparedStatement stmt=null;

		try {
			MyPool myPool=new MyPool(10);  ///自己寫的簡單連接池  
			conn=myPool.getConnection();
			  // 設置參數
			stmt=(PreparedStatement) conn.prepareStatement(sql);
	        ParameterMetaData parameterMetaData;
	        parameterMetaData = stmt.getParameterMetaData();
			  int count =parameterMetaData.getParameterCount();
			  for (int i = 1; i <= count; i++) {
		            stmt.setObject(i, obj[i - 1]);
		        }
		      stmt.executeUpdate();
		      conn.close();

  

《二》 文件下載

  文件下載dao模式和上傳的差不多 ,只是從數據庫中將文件名和文件路徑顯示出來,並作為下載時的傳遞參數給文件下載的servlet 這裏 我就只展示文件下載的servlet

  

下載傳遞的參數:

<a href="${pageContext.request.contextPath }/download?filepath=<%=list.get(i).getFilepath()%>&filename=<%=list.get(i).getFilename() %>">下載</a>

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		 //第一步:設置響應類型
	    resp.setContentType("application/force-download");//應用程序強制下載
	    //第二讀取文件
	  //  String path = getServletContext().getRealPath("/up/"+name);
	    String filename=req.getParameter("filename");
	    String filepath=req.getParameter("filepath");
	    System.out.println(filename+"   filename");
	    System.out.println(filepath+ "      filepath");
	    InputStream in = new FileInputStream(filepath);
	    //設置響應頭,對文件進行url編碼
	    filename = URLEncoder.encode(filename, "UTF-8");
	    resp.setHeader("Content-Disposition", "attachment;filename="+filename);   
	    //resp.setContentLength(in.available());
	    
	    //第三步:拷貝
	    OutputStream out = resp.getOutputStream();
	    byte[] b = new byte[1024];
	    int len = 0;
	    while((len = in.read(b))!=-1){
	      out.write(b, 0, len);
	    }
	    out.flush();
	    out.close();
	    in.close(); 
	    

  

在將上傳的文件路徑存入數據庫之前 課改替換掉路徑中的"\",故用到兩個方法replace(),replaceAll(),

replace和replaceAll是JAVA中常用的替換字符的方法,它們的區別是:

1)replace的參數是char和CharSequence,即可以支持字符的替換,也支持字符串的替換(CharSequence即字符串序列的意思,說白了也就是字符串);

2)replaceAll的參數是regex,即基於規則表達式的替換,比如,可以通過replaceAll("\\d", "*")把一個字符串所有的數字字符都換成星號;

相同點:都是全部替換,即把源字符串中的某一字符或字符串全部換成指定的字符或字符串,如果只想替換第一次出現的,可以使用replaceFirst(),這個方法也是基於規則表達式的替換,但與replaceAll()不同的是,只替換第一次出現的字符串;

另外,如果replaceAll()和replaceFirst()所用的參數據不是基於規則表達式的,則與replace()替換字符串的效果是一樣的,即這兩者也支持字符串的操作;

還有一點註意::執行了替換操作後,源字符串的內容是沒有發生改變的。

  舉例如下:  

String src = new String("ab43a2c43d");
 
System.out.println(src.replace("3","f"));=>ab4f2c4fd.
 
System.out.println(src.replace(‘3‘,‘f‘));=>ab4f2c4fd.
 
System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.
 
System.out.println(src.replaceAll("a","f"));=>fb43fc23d.
 
System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d
 
System.out.println(src.replaceFirst("4","h"));=>abh32c43d.

  

如何將字符串中的"\"替換成"\\": 

String msgIn;
 
String msgOut;
 
msgOut=msgIn.replaceAll("\\\\","\\\\\\\\");

原因:

  ‘\‘在java中是一個轉義字符,所以需要用兩個代表一個。例如System.out.println( "\\" ) ;只打印出一個"\"。但是‘\‘也是正則表達式中的轉義字符(replaceAll 的參數就是正則表達式),需要用兩個代表一個。所以:\\\\被java轉換成\\,\\又被正則表達式轉換成\。

將"\"替換成"/"

 String sqlPath="C:\Windows.old\Users";

sqlPath=sqlPath.replace("\\", "/");

文件上傳與下載