Sae 上传文件到Storage

来源:互联网 发布:网络电视无信号 编辑:程序博客网 时间:2024/06/05 18:14

首先说一下几个地方:

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


0 0
原创粉丝点击