客户端用的是 jquery file upload, 服务器端用的是restlet

package org.soachina.rest.component.resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.restlet.ext.fileupload.RestletFileUpload;
import org.restlet.representation.Representation;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource; public class PostDemoResource extends ServerResource {
@Post
public Representation post(Representation entity) {
// getResponse().setStatus(Status.SUCCESS_OK);
// return new StringRepresentation("11111111111" + parameters);
Representation rep = null;
// 1/ Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
//System.out.println("大头");
// 2/ Create a new file upload handler based on the Restlet
// FileUpload extension that will parse Restlet requests and
// generates FileItems.
RestletFileUpload upload = new RestletFileUpload(factory); // 3/ Request is parsed by the handler which generates a
// list of FileItems
List<FileItem> items = null;
try {
items = upload.parseRepresentation(entity);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} String filename = ""; for (FileItem fi : items) {
filename = fi.getName();
System.out.println("Save image ... " + filename);
/*新建一个图片文件*/
String extName=filename.substring(filename.lastIndexOf("."));
String newName=new SimpleDateFormat("yyyyMMDDHHmmssms").format(new Date());
File file=new File(newName+extName);
if(!file.exists()){//判断文件是否存在
try {
file.createNewFile(); //创建文件 } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*获取文件路径*/
String path_=file.getPath();
/*获取绝对路径名*/
String absPath=file.getAbsolutePath();
/*获取父亲文件路径*/
String parent=file.getParent();
try {
fi.write(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
return null;
}
}

restlet上传图片代码的更多相关文章

  1. ThinkPHP6 上传图片代码demo

    本文展示了ThinkPHP6 上传图片代码demo, 代码亲测可用. HTML部分代码 <tr> <th class="font-size-sm" style=& ...

  2. springmvc处理上传图片代码(校验图片尺寸、图片大小)

    package com.maizuo.web.controller; import com.maizuo.domain.Result; import com.maizuo.util.Constants ...

  3. PHP+七牛云存储上传图片代码片段

    2014年11月14日 16:37:51 第一段代码是上传单个图片的,第二个是上传多个图片的 //上传到七牛 //单个文件 //formname: 表单名字; pre: 图片Url中显示的图片名字(也 ...

  4. C#获取H5页面上传图片代码

    基于上一篇的H5压缩上传图片,由于图片是以二进制字符流blob的形式传过来的,所以应该想将其转成bytes类型再进行转换 public void ProcessRequest(HttpContext ...

  5. C# webApi上传图片 代码篇

    十年河东,十年河西,莫欺少年穷 学无止境,精益求精 代码篇,不多说 如下: using System; using System.Collections.Generic; using System.D ...

  6. 上传图片代码(chuantouxiang.php+touxiangchuli.php)

    <body> <form action="touxiangchuli.php" method="post" enctype="mul ...

  7. POST 方式上传图片

    Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data <form enctype="multipart/form-data&q ...

  8. laravel上传图片报错

    在laravel的上传图片代码文件中路径如下: vendor\stevenyangecho\laravel-u-editor\src\Uploader\Upload.php第131行有一句代码错误$r ...

  9. SpringMVC由浅入深day02_7上传图片

    7 上传图片 7.1 需求 在修改商品页面,添加上传商品图片功能. 7.2 springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data ...

随机推荐

  1. 总结一下js的原型和原型链

    最近学习了js的面向对象编程,原型和原型链这块是个难点,理解的不是很透彻,这里搜集了一些这方面的资料,以备复习所用 一. 原型与构造函数 Js所有的函数都有一个prototype属性,这个属性引用了一 ...

  2. Ubuntu14.04安装完全分布式Hadoop1.2.1

    一直想装hadoop集群,但总是没有时间,最近抽了三天时间下定决定装了一下,在我的wmware上虚拟了一台Ubuntu14.04,然后再复制了两台虚拟机,准备开始!!!! 基本参考 http://ww ...

  3. 第一次使用cnblogs

    第一次使用!!!!!留名纪念下!!!!!!!!!!!

  4. Xcode8注释有时会失效的解决方法

    1.苹果开发解决了xcode ghost,而且Xcode8也取消了三方插件的功能,所以注释有时会失效,解决办法终端运行命令:sudo /usr/libexec/xpccachectl  回车 +  输 ...

  5. Xcode-之项目重命名

    一.前言 在iOS开发过程中,对于以前的项目或者想改一下项目的名字,都会遇到比较麻烦的事情.直接改项目名,然而它会牵扯一大堆问题,并且不能把所有的名字都能改过来.有些文件改了项目名后,引导路径错误,导 ...

  6. Hibernate3 第四天

    Hibernate3 第四天 [第一天]三个准备七个步骤 [第二天]一级缓存.一级缓存快照.一对多和多对多配置 [第三天内容回顾] 1.各种查询 对象导航查询:配置信息不能出错, 根据OID查询:ge ...

  7. Android Studio Gradle project refresh failed No such property classpath for class

    新建的一个 android 项目居然发现不能运行,gradle 无法启动,奇怪: Gradle 'Meitian' project refresh failed:          No such p ...

  8. python中的pip安装

    windows下安装PIP 当前环境(windows 7,python安装路径为c:\Python) 1.首先到官网下载(https://pypi.python.org/pypi/setuptools ...

  9. POJ 3710 Christmas Game#经典图SG博弈

    http://poj.org/problem?id=3710 (说实话对于Tarjan算法在搞图论的时候就没搞太懂,以后得找时间深入了解) (以下有关无向图删边游戏的资料来自论文贾志豪<组合游戏 ...

  10. html中的a标签特例讲解

    将自己的博客写成了一个大杂烩了,遇见啥问题就写啥问题.但是当看见自己网页的成品就特别的开心. 还记得看见过的一个故事,说是收费的东西好还是免费的东西好,有一个答案是最让我记忆深刻的.回复的一个答案是: ...