• Sae 上传文件到Storage


    首先说一下几个地方:

    1.上传使用ss.upload("domin域名","源地址","目标地址,也就是storage的地址");假设要上传到storage某一个详细的文件夹下目标地址写为"upload/"+filename;文件就上传到了upload文件夹下。

    2.storage以下全部文件的路径是http://myapp-mybucket.stor.sinaapp.com/path/file.txt  前面是myapp是应用的名字,这个路径能够通过

     String realPath = ss.getUrl("域名", “上面的目标路径”); 就能够得到这个全网路径

    3.使用commons-fileupload上传组件时,先把文件写到一个暂时路径里,然后再写回storage就好了。

    以下是java代码:

    <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();
    				}
    			}
    		}


    
    

  • 相关阅读:
    底图的移动(setinterval 设置移动速度)
    Arrays的一点了解
    GestureDetector
    IO问题
    IO流
    Android动画
    Android中的Manager们
    Android对话框
    Android自定义View
    Android中关于Intent
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5348368.html
Copyright © 2020-2023  润新知