客户端用的是 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. 《学习的艺术》 (The Art of Learning)——划小圈 (Making Smaller Circles)

    “……我对象棋.武术以及范围更广的整个学习过程这三者的核心及相互关系的研究在一定程度上是受到了罗伯特.波席格<摩托车维护艺术>一书的启发.我决不会忘记指导我今后数年学习方式的那一幕.波席格 ...

  2. 【原创】Spring MVC项目搭建(使用Java配置)

    一.使用Intellij idea,新建maven项目,选择maven-archetype-webapp. 二.在src/main下新建文件夹,命名为java,并标注为source folder. 三 ...

  3. box2d 计算自由落体下一帧的位置

    var dt:Number=1/30; var y0:Number=_body.GetPosition().y; var y:Number=y0+_body.GetLinearVelocity().y ...

  4. iframe标签flash遮盖页面元素问题——wmode参数

    最近做项目过程中,遇到各种小问题,所以打算通过博客园来记录各问题的解决办法. 这篇的问题背景是这样子的:项目是用的AngularJS框架,在某个页面上用了wangEditor富文本编辑器插件(gith ...

  5. 暑假集训2016day3T1 欧拉回路(UOJ #117欧拉回路)(史上最全的欧拉回路纯无向图/有向图解析)

    原题……可惜不会……真是一只大蒟蒻…… ———————————————————————————————— 有一天一位灵魂画师画了一张图,现在要你找出欧拉回路,即在图中找一个环使得每条边都在环上出现恰好 ...

  6. [妙味JS基础]第一课:属性操作、图片切换、短信发送模拟

    知识点总结 HTML的属性操作:读.写 元素.属性名 => “读” 元素.属性名=新的值 => “写” 例如: oBtn.value => “读” oBtn.value='按钮' = ...

  7. POJ 2231 Moo Volume

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Farmer Jo ...

  8. 【C++】最大子列和

    此题来自<数据结构与算法>,书中一共介绍了四种方法,这里贴出两种. 1.分治递归,对本题来说,虽然有更好的算法,但是用此题理解分治算法感觉挺有用 #include <iostream ...

  9. HDU 2509 Be the Winner(取火柴博弈2)

    传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int ...

  10. C++ 内存分析-valgrind

    valgrind包括了以下几个比较重要的模块:memcheck, cachegrind, callgrind, helgrind, drd, massif, dhat, sgcheck, bbv. 还 ...