if(files!=null)
for(formbean file : files){
//filename为在服务端接收时希望保存成的文件名,filepath是本地文件路径(包括了源文件名),filebean中就包含了这俩属性
formparams.add(new filepart("file", file.getfilename(), new file(file.getfilepath())));
}
part[] parts = new part[formparams.size()];
iterator<art> pit = formparams.iterator();
int i=0;
while(pit.hasnext()){
parts[i++] = pit.next();
}
//如果出现乱码可以尝试一下方式
//stringpart sp = new stringpart("text", "testvalue", "gb2312");
//filepart fp = new filepart("file", "test.txt", new file("./temp/test.txt"), null, "gb2312"
//postmethod.getparams().setcontentcharset("gb2312");
multipartrequestentity mrp = new multipartrequestentity(parts, post.getparams());
post.setrequestentity(mrp);
//execute post method
httpclient client = new httpclient();
int code = client.executemethod(post);
system.out.println(code);
} catch ...
}
通过以上代码可以成功的模拟java客户端发送post请求,服务端也能接收并保存文件
java端测试的main方法:
string boundary = java.util.uuid.randomuuid().tostring();
string prefix = "--" , linend = "\r\n";
string multipart_from_data = "multipart/form-data";
string charset = "utf-8";
url uri = new url(actionurl);
httpurlconnection conn = (httpurlconnection) uri.openconnection();
conn.setreadtimeout(5 * 1000); // 缓存的最长时间
conn.setdoinput(true);// 允许输入
conn.setdooutput(true);// 允许输出
conn.setusecaches(false); // 不允许使用缓存
conn.setrequestmethod("post");
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("charsert", "utf-8");
conn.setrequestproperty("content-type", multipart_from_data + ";boundary=" + boundary);
// 首先组拼文本类型的参数
stringbuilder sb = new stringbuilder();
for (map.entry<string, string> entry : params.entryset()) {
sb.append(prefix);
sb.append(boundary);
sb.append(linend);
sb.append("content-disposition: form-data; name=\"" + entry.getkey() + "\"" + linend);
sb.append("content-type: text/plain; charset=" + charset+linend);
sb.append("content-transfer-encoding: 8bit" + linend);
sb.append(linend);
sb.append(entry.getvalue());
sb.append(linend);
}
dataoutputstream outstream = new dataoutputstream(conn.getoutputstream());
outstream.write(sb.tostring().getbytes());
// 发送文件数据
if(files!=null)
for (map.entry<string, file> file: files.entryset()) {
stringbuilder sb1 = new stringbuilder();
sb1.append(prefix);
sb1.append(boundary);
sb1.append(linend);
sb1.append("content-disposition: form-data; name=\"file\"; filename=\""+file.getkey()+"\""+linend);
sb1.append("content-type: application/octet-stream; charset="+charset+linend);
sb1.append(linend);
outstream.write(sb1.tostring().getbytes());
inputstream is = new fileinputstream(file.getvalue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outstream.write(buffer, 0, len);
}
is.close();
outstream.write(linend.getbytes());
}
//请求结束标志
byte[] end_data = (prefix + boundary + prefix + linend).getbytes();
outstream.write(end_data);
outstream.flush();
// 得到响应码
int res = conn.getresponsecode();
if (res == 200) {
inputstream in = conn.getinputstream();
int ch;
stringbuilder sb2 = new stringbuilder();
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
outstream.close();
conn.disconnect();
return in.tostring();
}
**********************
button响应中的代码:
**********************
***************************
服务器端servlet代码:
***************************
//print request.getinputstream to check request content
//httptool.printstreamcontent(request.getinputstream());
requestcontext req = new servletrequestcontext(request);
if(fileupload.ismultipartcontent(req)){
diskfileitemfactory factory = new diskfileitemfactory();
servletfileupload fileupload = new servletfileupload(factory);
fileupload.setfilesizemax(file_max_size);
list items = new arraylist();
try {
items = fileupload.parserequest(request);
} catch ...
iterator it = items.iterator();
while(it.hasnext()){
fileitem fileitem = (fileitem)it.next();
if(fileitem.isformfield()){
system.out.println(fileitem.getfieldname()+" "+fileitem.getname()+" "+new string(fileitem.getstring().getbytes("iso-8859-1"),"gbk"));
} else {
system.out.println(fileitem.getfieldname()+" "+fileitem.getname()+" "+
fileitem.isinmemory()+" "+fileitem.getcontenttype()+" "+fileitem.getsize());
if(fileitem.getname()!=null && fileitem.getsize()!=0){
file fullfile = new file(fileitem.getname());
file newfile = new file(file_save_path+fullfile.getname());
try {
fileitem.write(newfile);
} catch ...
} else {
system.out.println("no file choosen or empty file");
}
}
}
}
}
public void init() throws servletexception {
//读取在web.xml中配置的init-param
file_max_size = long.parselong(this.getinitparameter("file_max_size"));//上传文件大小限制
file_save_path = this.getinitparameter("file_save_path");//文件保存位置
}
如对本文有疑问, 点击进行留言回复!!
Android SDK Manager 不知为什么突然打不开了?看这里
监听Android系统发出的插入SD卡并且已正确安装(识别)时发出的广播
RecyclerView三种常用效果的实现方法-android-kotlin
Android Studio app启动错误 Error running :Default Activity not found
网友评论