zoukankan      html  css  js  c++  java
  • sea 上传文件

     1 public void doPost(HttpServletRequest request, HttpServletResponse response)
     2                         throws ServletException, IOException {
     3                 request.setCharacterEncoding("gbk");
     4                 PrintWriter out = response.getWriter();
     5                 //使用SaeUserInfo拿到改请求可写的路径
     6                 String realPath= SaeUserInfo.getSaeTmpPath()+"/"; 
     7                 try {
     8                         //使用common-upload上传文件至这个路径中
     9                      boolean isMultipart=ServletFileUpload.isMultipartContent(request);
    10                      if(!isMultipart)return;
    11                      DiskFileItemFactory factory=new DiskFileItemFactory();
    12                      ServletFileUpload upload=new ServletFileUpload(factory);
    13                      upload.setFileSizeMax(1024*1024);
    14                      List<FileItem> items=null;
    15                  items=upload.parseRequest(request);
    16                      for(FileItem item:items)
    17                      {
    18                        if(!item.isFormField())
    19                        {
    20                           File fullFile=new File(item.getName());
    21                           File uploadFile=new File(realPath,fullFile.getName());
    22                       item.write(uploadFile);
    23                       //上传完毕后 使用SaeStorage往storage里面写
    24                       SaeStorage ss = new SaeStorage();
    25                       //使用upload方法上传到域为image下
    26                       ss.upload("image", realPath+fullFile.getName(), fullFile.getName());
    27                       
    28                       out.print("upload file:"+realPath+fullFile.getName()+"</br>");
    29                        }
    30                      }
    31                      out.print("upload end...");
    32                 } catch (Exception e) {
    33                         out.print("ERROR:"+e.getMessage()+"</br>");
    34                 } finally{
    35                         out.flush();
    36                         out.close();
    37                 }
    38         }
     1  <form action="FileUpload" method="post" enctype="multipart/form-data">
     2       <table>
     3         <tr>
     4           <td>请选择要上传的文件</td>
     5           <td>
     6              <input type="file"name="filepath"size="20"/>
     7            </td>
     8         </tr>
     9         <tr>
    10            <td>
    11              <input type="submit"value="上传"/>
    12            </td>
    13         </tr>
    14        </table>
    15    </form>

    转载: http://saebbs.com/forum.php?mod=viewthread&tid=2367&highlight=Storage

  • 相关阅读:
    SQL Server中跨服务器跨数据库之间的数据增删改查
    Tomcat部署项目的方法
    java的位运算
    手机和邮箱格式验证
    Java实现List中某个对象属性中的字符串参数首字母进行排序
    springboot+dubbo+ZooKeeper+mybatis搭建分布式项目
    Java爬页面数据
    判断指定日期是否为节假日、双休日、工作日
    Java代码ping ip工具类
    Java生成压缩文件(zip、rar 格式
  • 原文地址:https://www.cnblogs.com/MDK-L/p/4463526.html
Copyright © 2011-2022 走看看