客户端用的是 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. ubuntu 10.4非法关机后上不了网

    用的好好的ubuntu 10.4,非法关机后居然上不了网,右上角的网络图标也不见了,还以为是网卡问题,进入xp,发现一切正常,心里不断地诅咒ubuntu,该死的ubuntu,我windows还天天非法 ...

  2. LEK-Introduction-Installation-Usage-new

    LEK is a set of tools which can take data from any source and search, analyze, and visualize it in r ...

  3. css后续属性

  4. 使用filter方法过滤集合元素

    文章转自https://my.oschina.net/nenusoul/blog/658238 Problem 你想要筛选出集合中的一些元素形成一个新的集合,这些元素都是满足你的筛选条件的. Solu ...

  5. 【转】使用sinopia五步快速完成本地npm搭建

    使用sinopia五步快速完成本地npm搭建 时间 2016-03-01 14:55:30  繁星UED 原文  http://ued.fanxing.com/shi-yong-sinopiawu-b ...

  6. j2ee tomcat 部署学习

    J2EE基础实例demo http://www.cnblogs.com/javabin/p/3809954.html J2EE 数据库JDBC(Java Data Base Connectivity, ...

  7. js-字符串函数

    js字符串函数 JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";v ...

  8. CF 604C Alternative Thinking#贪心

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...

  9. Qt_DX

    #ifndef MY_FRAME__HH__ #define MY_FRAME__HH__ #include <QtGui/QWidget> struct IDirect3D9; stru ...

  10. Inno setup 中 执行参数传递注意的地方

    Inno setup编译器编译使用pascal脚本编写的打包代码,其中Run段可以执行某些特定的程序,遇到一个bat批处理文件传递参数的问题,记录如下 1: [Run] 2: Filename: &q ...