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();
				}
			}
		}
				
时间: 2024-08-06 03:38:27

Sae 上传文件到Storage的相关文章

kindeditor在sae上传文件修改,适合php

kindeditor在sae上传文件修改,适合php 当前位置: 首页  > 论坛  > 经验共享 用户登录   新用户注册 主题: kindeditor在sae上传文件修改,适合php 作者: hforlove, 发布日期: 2013-05-31 17:57:53, 浏览数: 1017 upload_json.php 1. 在require_once 'JSON.php';后加入 if(isset($_SERVER['HTTP_APPNAME'])) $issae = true; //是否新

sae 上传文件 java实现

不讲废话上代码 1 /** 2 * 上传语音消息至sae服务器 3 * 4 * @param requestUrl 上传链接 5 * @param requestMethod http请求方式 6 */ 7 8 public static boolean httpRequestUploadVoice(String requestUrl, String requestMethod, String fileName){ 9 int byteread = 0; 10 boolean isUpload

上传文件到新浪云Storage的方法

上传文件到新浪云Storage的方法,兼容本地服务器 if (!empty($_FILES['sharepic']['name'])){ $tmp_file = $_FILES['sharepic']['tmp_name']; $file_types = explode(".", $_FILES['sharepic']['name']); $file_type = $file_types[count($file_types) - 1]; /*设置上传路径*/ if (isset($_S

【阿里云产品公测】ACE下上传文件永久存储实践

本帖主要内容: ;$,=VB:'   在阿里云的ACE下,我是如何实现让上传的文件永久保存的? ,%"!8T   本文以PHP为例,具体知识点如下: WD# 96V   第一,扩展服务“存储服务(Storage)”的入门基础: uzg(C#sp   第二,如何结合实际项目使用: \ne1Xu:hM   uP, iGA ?lq   2 j.6   ]8q#@%v}   写教程前,先发发牢骚吧! M.fAFL   我为什么要用云引擎ACE?因为我讨厌天天去更新系统.打补丁和查日志等等,这么专业的东西

linux socket c/s上传文件

这是上传文件的一个示例,可以参照自行修改成下载或者其它功能. 在上传时,需要先将文件名传到服务器端,这是采用一个结构体,包含文件名及文件名长度(可以用于校验),防止文件名乱码. client #include <stdio.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include "wrap.h" #define MAXLINE 1492 #de

JAVA模拟HTTP post请求上传文件

在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较麻烦的事情就是构造文件上传的HTTP报文格式,这个格式虽说也比较简单,但也比较容易出错.今天我们就一起来学习HTTP POST的报文格式以及通过Java来模拟文件上传的请求. 首先我们来看一个POST的报文请求,然后我们再来详细的分析它. POST报文格式 [plain] view plain co

用Azure CLI批量上传文件

在Windows环境下,我们可以使用AzCopy批量上传文件.其效率和传输速率都是非常快的. 在Linux或MacOS环境下,可以使用Azure的CLI实现批量文件的上传. 下面的脚本可以实现此功能. #!/bin/bash container=hwc btype=block storageaccount=hwtest storagekey=pBHrx8d+LDAkyHm2ffljPYygsiSBlbdQh8O45iV12BlFvdjI8kXbqtE17PlpCG0pfTU3yaBQUEEuWu

Fastdfs 无法上传文件问题描述及解决方案

今开发反馈一问题,发现fastdfs中有两个组无法上传文件,简单测试了下: fdfs_upload_file /etc/fdfs/storage.conf /root/1.jpg 会返回给我N00~N02,N04,N06~N10但缺少了N03,N05 通过fdfs_monitor和监控(23000端口)都是没问题的,于是专门对比了N02和N03,N05的区别之处: 发现N02的磁盘: /dev/sdc1             3.6T  884G  2.6T  26% /data/M00 /d

FastDfs 学习进阶之环境安装及上传文件测试

一:环境介绍       10.63.13.138 tracker,nginx proxy 10.63.13.143 storage,nginx 二:资源包准备 FastDFS官方论坛:http://www.csource.org 资源下载路径:http://sourceforge.net/projects/fastdfs/files/ 本实验学习环境使用CENTOS 6.4 X64 需要下载的资源包: FastDFS源代码:FastDFS_v5.05.tar.gz nginx模块源代码:fas