手机端的代码:
try {
// 实例化URL对象。调用URL有参构造方法,参数是一个url地址;
URL urlObj = new URL(url);
// 调用URL对象的openConnection()方法,创建HttpURLConnection对象;
httpConn = (HttpURLConnection) urlObj.openConnection();
// 调用HttpURLConnection对象setDoOutput(true)、setDoInput(true)、setRequestMethod("POST");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setRequestMethod("POST");
// 设置Http请求头信息;(Accept、Connection、Accept-Encoding、Cache-Control、Content-Type、User-Agent)
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Cache-Control", "no-cache");
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
httpConn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)");
// 调用HttpURLConnection对象的connect()方法,建立与服务器的真实连接;
httpConn.connect();
// 调用HttpURLConnection对象的getOutputStream()方法构建输出流对象;
dos = new DataOutputStream(httpConn.getOutputStream());
// 获取表单中上传控件之外的控件数据,写入到输出流对象(根据HttpWatch提示的流信息拼凑字符串);
if (map != null && !map.isEmpty()) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = map.get(key);
dos.writeBytes(PREFIX + BOUNDARY + NEWLINE);
dos.writeBytes("Content-Disposition: form-data; "
+ "name=\"" + key + "\"" + NEWLINE);
dos.writeBytes(NEWLINE);
dos.writeBytes(URLEncoder.encode(value.toString(), charset));
dos.writeBytes(NEWLINE);
}
}
// 获取表单中上传控件的数据,写入到输出流对象(根据HttpWatch提示的流信息拼凑字符串);
if (body_data != null && body_data.length > 0) {
dos.writeBytes(PREFIX + BOUNDARY + NEWLINE);
String fileName = filePath.substring(filePath
.lastIndexOf(File.separatorChar));
dos.writeBytes("Content-Disposition: form-data; " + "name=\""
+ fileType + "\"" + "; filename=\"" + fileName
+ "\"" + NEWLINE);
dos.writeBytes(NEWLINE);
dos.write(body_data);
dos.writeBytes(NEWLINE);
}
dos.writeBytes(PREFIX + BOUNDARY + PREFIX + NEWLINE);
dos.flush();
服务器端代码:
ioc代码如下:
var ioc = {
utils : {
type : 'cn.guagua.mobile.common.avatarUploadUtils',
fields : {
sc : {app:'$servlet'} // 将 ServletContext 对象注入 MyUtils
}
},
tmpFilePool : {
type : 'org.nutz.filepool.NutFilePool', // 临时文件最大个数为 1000 个
args : [ '~/opt/uploadlog/avatar/tmps', 1000 ]
},
uploadMediaContext : {
type : 'org.nutz.mvc.upload.UploadingContext',
singleton : false,
args : [ { refer : 'tmpFilePool' } ],
fields : {
// 是否忽略空文件, 默认为 false
ignoreNull : true,
// 单个文件最大尺寸(大约的值,单位为字节,即 1048576 为 1M)
maxFileSize : 31457280,
nameFilter : '^(.+[.])(gif|jpg|png)$' // 正则表达式匹配可以支持的文件名
}
},
myMediaUpload : {
type : 'org.nutz.mvc.upload.UploadAdaptor',
singleton : false,
args : [ { refer : 'uploadMediaContext' }]
}
};
@At("/avatarupload")
@AdaptBy(type = UploadAdaptor.class,args = { "ioc:myMediaUpload" })
public Map<?, ?> avatarUpload(HttpServletRequest request,@Param("..")NutMap nm){
TempFile tf = (TempFile)nm.get("imgBig");
FieldMeta fm = tf.getMeta();
String ext_type = fm.getFileExtension();
String content_type = fm.getContentType();
图片流能正常接收,但是content_type 从手机端传过来是的是nul , 请问这是什么原因?