restlet上传图片代码
客户端用的是 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上传图片代码的更多相关文章
- ThinkPHP6 上传图片代码demo
本文展示了ThinkPHP6 上传图片代码demo, 代码亲测可用. HTML部分代码 <tr> <th class="font-size-sm" style=& ...
- springmvc处理上传图片代码(校验图片尺寸、图片大小)
package com.maizuo.web.controller; import com.maizuo.domain.Result; import com.maizuo.util.Constants ...
- PHP+七牛云存储上传图片代码片段
2014年11月14日 16:37:51 第一段代码是上传单个图片的,第二个是上传多个图片的 //上传到七牛 //单个文件 //formname: 表单名字; pre: 图片Url中显示的图片名字(也 ...
- C#获取H5页面上传图片代码
基于上一篇的H5压缩上传图片,由于图片是以二进制字符流blob的形式传过来的,所以应该想将其转成bytes类型再进行转换 public void ProcessRequest(HttpContext ...
- C# webApi上传图片 代码篇
十年河东,十年河西,莫欺少年穷 学无止境,精益求精 代码篇,不多说 如下: using System; using System.Collections.Generic; using System.D ...
- 上传图片代码(chuantouxiang.php+touxiangchuli.php)
<body> <form action="touxiangchuli.php" method="post" enctype="mul ...
- POST 方式上传图片
Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data <form enctype="multipart/form-data&q ...
- laravel上传图片报错
在laravel的上传图片代码文件中路径如下: vendor\stevenyangecho\laravel-u-editor\src\Uploader\Upload.php第131行有一句代码错误$r ...
- SpringMVC由浅入深day02_7上传图片
7 上传图片 7.1 需求 在修改商品页面,添加上传商品图片功能. 7.2 springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data ...
随机推荐
- Jquery - UI - Dialog(转)
jQuery UI Dialog常用的参数有: 1.autoOpen:默认true,即dialog方法创建就显示对话框 2.buttons:默认无,用于设置显示的按钮,可以是JSON和Array形式: ...
- JQuery中$.ajax()方法参数详解及应用
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- <?php function say() { echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()
<?php function say() { echo 'hello world'; } //在这里调用函数 say(); php 调用方法say()
- bzoj4318: OSU!&&CF235BLet's Play Osu!
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4318 4318: OSU! Time Limit: 2 Sec Memory Limit ...
- top.location != self.location
top.location != self.location 就是说当前窗体的url和父窗体的 url是不是相同 这个是为了防止别的网站嵌入你的网站的内容(比如用iframe嵌入的你的网站的页面)
- 取URL得值
有这样一个URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e,请写一段JS程序提取URL中的各个GET参数(参 ...
- 简单介绍移动端CSS3单位rem的用法
PC端大部份是用px单位,小部分用em单位,而移动端,请全部用rem单位吧.目前大部份设备,包括但不限于iOS 5+.Android 2.3+.Window Phone 8+都是可以兼容的,具体兼容表 ...
- apicloud 初学
html5:在创建html时为了防止页面缩放等不兼容效果,要创建个viewport <meta name="viewport" content="maximum-s ...
- C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)
1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库Boost 3.C++各大有名库的介绍--GUI 4.C++各大有名库的介绍--网络通信 5.C++各大有名库的介绍- ...
- NOIP2010-普及组复赛-第四题-三国游戏
题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不 ...