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 ...
随机推荐
- BuildingAndRunningUAFServerUsingMaven
https://github.com/eBay/UAF/wiki/BuildingAndRunningUAFServerUsingMaven(CLIonly) 实现uaf的demo,使用ebay的方案 ...
- WebApi Help Pages
如何新建Help Pages在此不多复述,网上很多: https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/ ...
- JSON.parse() JSON.stringify() eval() jQuery.parseJSON() 的区别
http://www.jb51.net/article/81880.htm : jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的Java ...
- sublime text3编译运行C,Java程序的一些配置
环境:linux 64位 桌面环境: gnome Java编译运行 (1)Preferences --> Browse Packages --> 在该文件夹下新建build文件如: Myj ...
- 设置radio的选中状态
$("#s7").click(function () { var a = document.getElementById("s7"); if (a.checke ...
- HDU 4247 A Famous ICPC Team
Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw ...
- ubuntu环境下docker安装步骤
本文是根据docker官方文档翻译,原文:https://docs.docker.com/engine/installation/linux/ubuntulinux/ Docker 支持以下 Ubun ...
- Java 序列化 transient关键字
Java 序列化 transient关键字 @author 敏敏Alexia 转自:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html 1. tra ...
- 安卓访问webAPI,并取回数据
前言 安卓自从4.0以后,所有的网络访问都需要异步进程操作.其自带的异步类有AsyncTask,Handler,以及可以声明Thread等等.涉及到多进程,必须要提到一个问题,线程与线程之间不能直接进 ...
- java ee开发报错
七月 26, 2015 9:57:52 下午 org.apache.coyote.AbstractProtocol destroy信息: Destroying ProtocolHandler [&qu ...