mimmime type imagee/jpeg是什么图片

在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传 - 实践 + 参考 - ITeye技术网站
博客分类:
通过构造基于 HTTP 协议的传输内容实现图片自动上传到服务器功能 。如果自己编码构造 HTTP 协议,那么编写的代码质量肯定不高,建议模仿
\mime\ClientMultipartFormPost.java 来实现,并通过源码来进一步理解如何优雅高效地构造 HTTP 协议传输内容。
自己构造 HTTP
协议传输内容的想法,从何而来呢?灵感启迪于这篇博文“
”,以前从未想过通过抓取
HTTP 请求数据格式,根据协议自己构造数据来实现数据提交。哎,Out 了。因为 Apache HttpClient
框架就是通过此方式来实现的,以前从未注意到,看来以后要多多向前人学习啊!结果是:阅读了此框架的源码后,才知道自己编写的代码和人家相比真不是一个档次的。现在已经下定决心了,多读开源框架代码,不但可以熟悉相关业务流程,而且还可以学到设计模式在实际业务需求中的应用,更重要的是领悟其中的思想。业务流程、实践能力、框架思想,一举三得,何乐而不为呢。^_^
test.html 部分源码:
&form action="Your_Action_Url
" method="post" enctype="multipart/form-data
" name="form1" id="form1"&
&label for="upload_file"&&/label&
&input type="file" name="upload_file" id="upload_file
&input type="submit" name="action" id="action
" value="upload
通过 HttpWatch 查看抓取到的包数据格式:
下面将分别通过按照 HttpWatch 抓取下来的协议格式内容构造传输内容实现文件上传功能和基于 HttpClient 框架实现文件上传功能。
项目配置目录Your_Project/config
,相关文件
actionUrl.properties 文件内容:
Your_Action_Url
formDataParams.properties 文件内容(对应 HTML Form 属性内容):
imageParams.properties 文件内容(这里文件路径已配置死了,不好!建议在程序中动态设置,即通过传入相关参数实现。):
upload_file
=images/roewe.jpg
MIMETypes.properties 文件内容(参考自
jpeg:image/jpeg
jpg:image/jpeg
png:image/png
gif:image/gif
》代码的基础上,通过进一步改进得到如下代码(Java、Android 都可以 run):
* 文件名称:UploadImage.java
* 版权信息:Apache License, Version 2.0
* 功能描述:实现图片文件上传。
* 创建日期:
* 作者:Bert Lee
* 修改历史:
public class UploadImage {
String multipart_form_data = "multipart/form-data";
String twoHyphens = "--";
String boundary = "****************fD4fH3gL0hK7aI6";
// 数据分隔符
String lineEnd = System.getProperty("line.separator");
// The value is "\r\n" in Windows.
* 上传图片内容,格式请参考HTTP 协议格式。
* 人人网Photos.upload中的”程序调用“http://wiki./wiki/Photos.upload#.E7.A8.8B.E5.BA.8F.E8.B0.83.E7.94.A8
* 对其格式解释的非常清晰。
* 格式如下所示:
* --****************fD4fH3hK7aI6
* Content-Disposition: form- name="upload_file"; filename="apple.jpg"
* Content-Type: image/jpeg
* 这儿是文件的内容,二进制流的形式
private void addImageContent(Image[] files, DataOutputStream output) {
for(Image file : files) {
StringBuilder split = new StringBuilder();
split.append(twoHyphens + boundary + lineEnd);
split.append("Content-Disposition: form- name=\"" + file.getFormName() + "\"; filename=\"" + file.getFileName() + "\"" + lineEnd);
split.append("Content-Type: " + file.getContentType() + lineEnd);
split.append(lineEnd);
// 发送图片数据
output.writeBytes(split.toString());
output.write(file.getData(), 0, file.getData().length);
output.writeBytes(lineEnd);
} catch (IOException e) {
throw new RuntimeException(e);
* 构建表单字段内容,格式请参考HTTP 协议格式(用FireBug可以抓取到相关数据)。(以便上传表单相对应的参数值)
* 格式如下所示:
* --****************fD4fH3hK7aI6
* Content-Disposition: form- name="action"
* // 一空行,必须有
private void addFormField(Set&Map.Entry&Object,Object&& params, DataOutputStream output) {
StringBuilder sb = new StringBuilder();
for(Map.Entry&Object, Object& param : params) {
sb.append(twoHyphens + boundary + lineEnd);
sb.append("Content-Disposition: form- name=\"" + param.getKey() + "\"" + lineEnd);
sb.append(lineEnd);
sb.append(param.getValue() + lineEnd);
output.writeBytes(sb.toString());// 发送表单字段数据
} catch (IOException e) {
throw new RuntimeException(e);
* 直接通过 HTTP 协议提交数据到服务器,实现表单提交功能。
* @param actionUrl 上传路径
* @param params 请求参数key为参数名,value为参数值
* @param files 上传文件信息
* @return 返回请求结果
public String post(String actionUrl, Set&Map.Entry&Object,Object&& params, Image[] files) {
HttpURLConnection conn =
DataOutputStream output =
BufferedReader input =
URL url = new URL(actionUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(120000);
conn.setDoInput(true);
// 允许输入
conn.setDoOutput(true);
// 允许输出
conn.setUseCaches(false);
// 不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Content-Type", multipart_form_data + "; boundary=" + boundary);
conn.connect();
output = new DataOutputStream(conn.getOutputStream());
addImageContent(files, output);
// 添加图片内容
addFormField(params, output);
// 添加表单字段内容
output.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);// 数据结束标志
output.flush();
int code = conn.getResponseCode();
if(code != 200) {
throw new RuntimeException("请求‘" + actionUrl +"’失败!");
input = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String oneL
while((oneLine = input.readLine()) != null) {
response.append(oneLine + lineEnd);
return response.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 统一释放资源
if(output != null) {
output.close();
if(input != null) {
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
if(conn != null) {
conn.disconnect();
public static void main(String[] args) {
String response = "";
BufferedReader in = new BufferedReader(new FileReader("config/actionUrl.properties"));
String actionUrl = in.readLine();
// 读取表单对应的字段名称及其值
Properties formDataParams = new Properties();
formDataParams.load(new FileInputStream(new File("config/formDataParams.properties")));
Set&Map.Entry&Object,Object&& params = formDataParams.entrySet();
// 读取图片所对应的表单字段名称及图片路径
Properties imageParams = new Properties();
imageParams.load(new FileInputStream(new File("config/imageParams.properties")));
Set&Map.Entry&Object,Object&& images = imageParams.entrySet();
Image[] files = new Image[images.size()];
int i = 0;
for(Map.Entry&Object,Object& image : images) {
Image file = new Image(image.getValue().toString(), image.getKey().toString());
files[i++] =
Image file = new Image("images/apple.jpg", "upload_file");
Image[] files = new Image[0];
files[0] =
response = new UploadImage().post(actionUrl, params, files);
System.out.println("返回结果:" + response);
} catch (IOException e) {
e.printStackTrace();
2. 基于 HttpClient 框架实现文件上传,实例代码如下:
* 文件名称:ClientMultipartFormPost.java
* 版权信息:Apache License, Version 2.0
* 功能描述:通过 HttpClient 4.1.1 实现文件上传。
* 创建日期:
* 作者:Bert Lee
* 修改历史:
public class ClientMultipartFormPost {
* 直接通过 HttpMime's MultipartEntity 提交数据到服务器,实现表单提交功能。
* @return Post 请求所返回的内容
public static String filePost() {
HttpClient httpclient = new DefaultHttpClient();
BufferedReader in = new BufferedReader(new FileReader("config/actionUrl.properties"));
String actionU
actionUrl = in.readLine();
HttpPost httppost = new HttpPost(actionUrl);
// 通过阅读源码可知,要想实现图片上传功能,必须将 MultipartEntity 的模式设置为 BROWSER_COMPATIBLE 。
MultipartEntity multiEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
MultipartEntity multiEntity = new MultipartEntity();
// 读取图片的 MIME Type 类型集
Properties mimeTypes = new Properties();
mimeTypes.load(new FileInputStream(new File("config/MIMETypes.properties")));
// 构造图片数据
Properties imageParams = new Properties();
imageParams.load(new FileInputStream(new File("config/imageParams.properties")));
String fileT
for(Map.Entry&Object,Object& image : imageParams.entrySet()) {
String path = image.getValue().toString();
fileType = path.substring(path.lastIndexOf(".") + 1);
FileBody binaryContent = new FileBody(new File(path), mimeTypes.get(fileType).toString());
FileBody binaryContent = new FileBody(new File(path));
multiEntity.addPart(image.getKey().toString(), binaryContent);
// 构造表单参数数据
Properties formDataParams = new Properties();
formDataParams.load(new FileInputStream(new File("config/formDataParams.properties")));
for(Entry&Object, Object& param : formDataParams.entrySet()) {
multiEntity.addPart(param.getKey().toString(), new StringBody(param.getValue().toString()));
httppost.setEntity(multiEntity);
Out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Out.println("-------------------");
Out.println(response.getStatusLine());
if(resEntity != null) {
String returnContent = EntityUtils.toString(resEntity);
EntityUtils.consume(resEntity);
return returnC // 返回页面内容
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
httpclient.getConnectionManager().shutdown();
public static void main(String[] args) {
Out.println("Response content: " + ClientMultipartFormPost.filePost());
参考资料:
下载次数: 563
浏览 13533
浏览: 25800 次
来自: 杭州基本信息(imageInfo) | 七牛云存储
图片基本信息(imageInfo)
图片基本信息包括图片格式、图片大小、色彩模型。
在图片下载URL后附加imageInfo指示符(区分大小写),即可获取JSON格式的图片基本信息。
请求报文格式
GET &ImageDownloadURI&?imageInfo HTTP/1.1
Host: &ImageDownloadHost&
注意:当您下载私有空间的资源时,ImageDownloadURI的生成方法请参考七牛的。
资源为/resource/gogopher.jpg,处理样式为imageInfo。
#构造下载URL
DownloadUrl = '/resource/gogopher.jpg?imageInfo'
RealDownloadUrl = '/resource/gogopher.jpg?imageInfo&e=×××&token=MY_ACCESS_KEY:×××'
下载服务器域名,可为七牛三级域名或自定义二级域名,参考
响应报文格式
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
&&ImageType
&ImageWidth
&ImageHeight
&colorModel&:
&&ImageColorModel
&frameNumber&:
&ImageFrameNumber
Content-Type
MIME类型,固定为application/json
Cache-Control
缓存控制,固定为no-store,不缓存
■ 如果请求成功,返回包含如下内容的JSON字符串(已格式化,便于阅读):
&&ImageType
&ImageWidth
&ImageHeight
&colorModel&:
&&ImageColorModel
&frameNumber&:
&ImageFrameNumber
图片类型,如png、jpeg、gif、bmp等。
图片宽度,单位:像素(px)。
图片高度,单位:像素(px)。
colorModel
彩色空间,如palette16、ycbcr等。
frameNumber
帧数,gif 图片会返回此项。
■ 如果请求失败,返回包含如下内容的JSON字符串(已格式化,便于阅读):
HTTP状态码,请参考
与HTTP状态码对应的消息文本
响应状态码
HTTP状态码
请求报文格式错误
资源不存在
服务端操作失败。如遇此错误,请将完整错误信息(包括所有HTTP响应头部)给我们。
获取图片基本信息:
在Web浏览器中输入以下图片地址
/resource/gogopher.jpg?imageInfo
返回结果(内容经过格式化以便阅读)
&colorModel&:
内部参考资源
为您找到了如下结果,感谢您对七牛的支持。
技术/购买咨询
400-808-9176
开发者支持当前访客身份:游客 [
当前位置:
发布于 日 11时,
根据自定义的目标图高宽,&按目标图高宽比裁剪、缩放源图.传入参数:$source_path&string&源图路径$target_width&integer&目标图宽度$target_height&integer&目标图高度源图支持MIMETYPE:&image/gif,&image/jpeg,&image/png.
代码片段(1)
1.&[代码][PHP]代码&&&&
function imagecropper($source_path, $target_width, $target_height)
$source_info
= getimagesize($source_path);
$source_width
= $source_info[0];
$source_height = $source_info[1];
$source_mime
= $source_info['mime'];
$source_ratio
= $source_height / $source_
$target_ratio
= $target_height / $target_
// 源图过高
if ($source_ratio & $target_ratio)
$cropped_width
= $source_
$cropped_height = $source_width * $target_
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
// 源图过宽
elseif ($source_ratio & $target_ratio)
$cropped_width
= $source_height / $target_
$cropped_height = $source_
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
// 源图适中
$cropped_width
= $source_
$cropped_height = $source_
$source_x = 0;
$source_y = 0;
switch ($source_mime)
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
case 'image/png':
$source_image = imagecreatefrompng($source_path);
$target_image
= imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
header('Content-Type: image/jpeg');
imagejpeg($target_image);
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
开源中国-程序员在线工具:
相关的代码(105)
25回/7113阅
1回/1255阅
开源从代码分享开始
sidbusy的其它代码MIME 参考手册
MIME 参考手册
MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。
MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。
官方的 MIME 信息是由 Internet Engineering Task Force (IETF) 在下面的文档中提供的:
Standard for
ARPA Internet text messages
MIME Part 1: Format of Internet Message Bodies
MIME Part 2: Media Types
MIME Part 3: Header Extensions for Non-ASCII Text
MIME Part 4: Registration Procedures
MIME Part 5: Conformance Criteria and Examples
不同的应用程序支持不同的 MIME 类型。
下面的参考手册是由 Microsoft Internet Information Server version 5 所支持的 MIME 类型列表。
按照内容类型排列的 Mime 类型列表
类型/子类型
application/envoy
application/fractals
application/futuresplash
application/hta
application/internet-property-stream
application/mac-binhex40
application/msword
application/msword
application/octet-stream
application/octet-stream
application/octet-stream
application/octet-stream
application/octet-stream
application/octet-stream
application/octet-stream
application/oda
application/olescript
application/pdf
application/pics-rules
application/pkcs10
application/pkix-crl
application/postscript
application/postscript
application/postscript
application/rtf
application/set-payment-initiation
application/set-registration-initiation
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-outlook
application/vnd.ms-pkicertstore
application/vnd.ms-pkiseccat
application/vnd.ms-pkistl
application/vnd.ms-powerpoint
application/vnd.ms-powerpoint
application/vnd.ms-powerpoint
application/vnd.ms-project
application/vnd.ms-works
application/vnd.ms-works
application/vnd.ms-works
application/vnd.ms-works
application/winhlp
application/x-bcpio
application/x-cdf
application/x-compress
application/x-compressed
application/x-cpio
application/x-csh
application/x-director
application/x-director
application/x-director
application/x-dvi
application/x-gtar
application/x-gzip
application/x-hdf
application/x-internet-signup
application/x-internet-signup
application/x-iphone
application/x-javascript
application/x-latex
application/x-msaccess
application/x-mscardfile
application/x-msclip
application/x-msdownload
application/x-msmediaview
application/x-msmediaview
application/x-msmediaview
application/x-msmetafile
application/x-msmoney
application/x-mspublisher
application/x-msschedule
application/x-msterminal
application/x-mswrite
application/x-netcdf
application/x-netcdf
application/x-perfmon
application/x-perfmon
application/x-perfmon
application/x-perfmon
application/x-perfmon
application/x-pkcs12
application/x-pkcs12
application/x-pkcs7-certificates
application/x-pkcs7-certificates
application/x-pkcs7-certreqresp
application/x-pkcs7-mime
application/x-pkcs7-mime
application/x-pkcs7-signature
application/x-sh
application/x-shar
application/x-shockwave-flash
application/x-stuffit
application/x-sv4cpio
application/x-sv4crc
application/x-tar
application/x-tcl
application/x-tex
application/x-texinfo
application/x-texinfo
application/x-troff
application/x-troff
application/x-troff
application/x-troff-man
application/x-troff-me
application/x-troff-ms
application/x-ustar
application/x-wais-source
application/x-x509-ca-cert
application/x-x509-ca-cert
application/x-x509-ca-cert
application/ynd.ms-pkipko
application/zip
audio/basic
audio/basic
audio/mpeg
audio/x-aiff
audio/x-aiff
audio/x-aiff
audio/x-mpegurl
audio/x-pn-realaudio
audio/x-pn-realaudio
audio/x-wav
image/cis-cod
image/jpeg
image/jpeg
image/jpeg
image/pipeg
image/svg+xml
image/tiff
image/tiff
image/x-cmu-raster
image/x-cmx
image/x-icon
image/x-portable-anymap
image/x-portable-bitmap
image/x-portable-graymap
image/x-portable-pixmap
image/x-rgb
image/x-xbitmap
image/x-xpixmap
image/x-xwindowdump
message/rfc822
message/rfc822
message/rfc822
text/plain
text/plain
text/plain
text/plain
text/richtext
text/scriptlet
text/tab-separated-values
text/webviewhtml
text/x-component
text/x-setext
text/x-vcard
video/mpeg
video/mpeg
video/mpeg
video/mpeg
video/mpeg
video/mpeg
video/quicktime
video/quicktime
video/x-la-asf
video/x-la-asf
video/x-ms-asf
video/x-ms-asf
video/x-ms-asf
video/x-msvideo
video/x-sgi-movie
x-world/x-vrml
x-world/x-vrml
x-world/x-vrml
x-world/x-vrml
x-world/x-vrml
x-world/x-vrml
按照文件扩展名排列的 Mime 类型列表
类型/子类型
application/octet-stream
application/internet-property-stream
application/postscript
audio/x-aiff
audio/x-aiff
audio/x-aiff
video/x-ms-asf
video/x-ms-asf
video/x-ms-asf
audio/basic
video/x-msvideo
application/olescript
text/plain
application/x-bcpio
application/octet-stream
text/plain
application/vnd.ms-pkiseccat
application/x-cdf
application/x-x509-ca-cert
application/octet-stream
application/x-msclip
image/x-cmx
image/cis-cod
application/x-cpio
application/x-mscardfile
application/pkix-crl
application/x-x509-ca-cert
application/x-csh
application/x-director
application/x-x509-ca-cert
application/x-director
application/x-msdownload
application/octet-stream
application/msword
application/msword
application/x-dvi
application/x-director
application/postscript
text/x-setext
application/envoy
application/octet-stream
application/fractals
x-world/x-vrml
application/x-gtar
application/x-gzip
text/plain
application/x-hdf
application/winhlp
application/mac-binhex40
application/hta
text/x-component
text/webviewhtml
image/x-icon
application/x-iphone
application/x-internet-signup
application/x-internet-signup
image/pipeg
image/jpeg
image/jpeg
image/jpeg
application/x-javascript
application/x-latex
application/octet-stream
video/x-la-asf
video/x-la-asf
application/octet-stream
application/x-msmediaview
application/x-msmediaview
audio/x-mpegurl
application/x-troff-man
application/x-msaccess
application/x-troff-me
message/rfc822
message/rfc822
application/x-msmoney
video/quicktime
video/x-sgi-movie
video/mpeg
audio/mpeg
video/mpeg
video/mpeg
video/mpeg
video/mpeg
application/vnd.ms-project
video/mpeg
application/x-troff-ms
application/x-msmediaview
message/rfc822
application/oda
application/pkcs10
application/x-pkcs12
application/x-pkcs7-certificates
application/x-pkcs7-mime
application/x-pkcs7-mime
application/x-pkcs7-certreqresp
application/x-pkcs7-signature
image/x-portable-bitmap
application/pdf
application/x-pkcs12
image/x-portable-graymap
application/ynd.ms-pkipko
application/x-perfmon
application/x-perfmon
application/x-perfmon
application/x-perfmon
application/x-perfmon
image/x-portable-anymap
application/vnd.ms-powerpoint
image/x-portable-pixmap
application/vnd.ms-powerpoint
application/vnd.ms-powerpoint
application/pics-rules
application/postscript
application/x-mspublisher
video/quicktime
audio/x-pn-realaudio
audio/x-pn-realaudio
image/x-cmu-raster
image/x-rgb
application/x-troff
application/rtf
text/richtext
application/x-msschedule
text/scriptlet
application/set-payment-initiation
application/set-registration-initiation
application/x-sh
application/x-shar
application/x-stuffit
audio/basic
application/x-pkcs7-certificates
application/futuresplash
application/x-wais-source
application/vnd.ms-pkicertstore
application/vnd.ms-pkistl
image/svg+xml
application/x-sv4cpio
application/x-sv4crc
application/x-shockwave-flash
application/x-troff
application/x-tar
application/x-tcl
application/x-tex
application/x-texinfo
application/x-texinfo
application/x-compressed
image/tiff
image/tiff
application/x-troff
application/x-msterminal
text/tab-separated-values
text/plain
application/x-ustar
text/x-vcard
x-world/x-vrml
audio/x-wav
application/vnd.ms-works
application/vnd.ms-works
application/vnd.ms-works
application/x-msmetafile
application/vnd.ms-works
application/x-mswrite
x-world/x-vrml
x-world/x-vrml
x-world/x-vrml
image/x-xbitmap
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
application/vnd.ms-excel
x-world/x-vrml
image/x-xpixmap
image/x-xwindowdump
application/x-compress
application/zip}

我要回帖

更多关于 imagejpeg 不输出 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信