Sae 上傳文件到Storage

首先說一下幾個地方:java

1.上傳使用ss.upload("domin域名","源地址","目標地址,也就是storage的地址");假設要上傳到storage某一個詳細的文件夾下目標地址寫爲"upload/"+filename;文件就上傳到了upload文件夾下。session

2.storage如下所有文件的路徑是http://myapp-mybucket.stor.sinaapp.com/path/file.txt  前面是myapp是應用的名字,這個路徑可以經過app

 String realPath = ss.getUrl("域名", 「上面的目標路徑」); 就可以獲得這個全網路徑
dom

3.使用commons-fileupload上傳組件時,先把文件寫到一個暫時路徑裏,而後再寫回storage就行了。spa

如下是java代碼:code

<pre name="code" class="java">private void userSave(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		FileItemFactory factory=new DiskFileItemFactory();
		ServletFileUpload upload=new ServletFileUpload(factory);
		List<FileItem> items=null;
		try {
			items=upload.parseRequest(request);
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator<FileItem> itr=items.iterator();
		
		HttpSession session=request.getSession();
		
		User user=(User)session.getAttribute("currentUser");
		//上傳到Storage以後的真是路徑
		String realPath=user.getImageName();
		String imageName=user.getImageName();
		boolean imageChange=false;
<span style="white-space:pre">		</span>while(itr.hasNext()){  
          <span style="white-space:pre">	</span>  FileItem item=(FileItem)itr.next();  
           <span style="white-space:pre">	</span> if(!item.isFormField()){  
            <span style="white-space:pre">	</span>  try{  
              <span style="white-space:pre">	</span>    imageName=DateUtil.getCurrentDateStr();  
               <span style="white-space:pre">	</span> //帶後綴的文件名稱  
             <span style="white-space:pre">	</span>   imageName=imageName+"."+item.getName().split("\\.")[1];  
             <span style="white-space:pre">	</span>   user.setImageName(imageName);  
            <span style="white-space:pre">	</span>    //String filePath=PropertiesUtil.getValue("imagePath")+imageName+"."+item.getName().split("\\.")[1];  
            <span style="white-space:pre">	</span>    String folder=PropertiesUtil.getValue("imagePath");  
           <span style="white-space:pre">	</span>    String filePath=session.getServletContext().getRealPath(folder)+"/"+imageName;  
<span style="white-space:pre">		</span>   <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">//前面的部分僅僅需要按正常的上傳來寫就可以了 filePath僅僅是一個暫時文件</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">  </span>
<span style="white-space:pre">		</span>   <span style="color:#ff0000;">item.write(new File(filePath));</span>
		    // 上傳完成後 使用SaeStorage往storage裏面寫
                    SaeStorage ss = new SaeStorage();
                    // 使用upload方法上傳到域domain下,此處本人的是onway
                    <span style="color:#ff0000;">ss.upload("onway", filePath, <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">"upload/"</span>+imageName);</span>
                    // 獲取上傳後的圖片路徑
                    realPath = <span style="color:#ff0000;">ss.getUrl</span>("onway", <span style="color: rgb(0, 0, 255); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">"upload/"</span>+imageName);
                   // System.out.println(realPath);
		}catch(Exception e){
		   e.printStackTrace();
				}
			}
		}
相關文章
相關標籤/搜索