java 表单上传
来源:互联网 发布:php 会员登录获取id 编辑:程序博客网 时间:2024/06/03 04:48
String end = "\r\n";
String twoHyphens = "--";
String boundary = "-------------------------7de16713908a8";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
con.setRequestProperty("Accept-Encoding", "gzip,deflate");
con.setRequestProperty("Accept-Language", "zh-CN");
con.setRequestProperty("Cache-Control", "no-cache");
// con.setRequestProperty("uptype", "client");
String fileType = getFileType(uploadFile);
String newName = getFileName(uploadFile);
String content_disposition = "Content-Disposition: form-data; " +
"name=\"upfile\"; filename=\"" +
newName +".text" +"\"" + end;
String content_type = "Content-Type: text/plain"+end;
String content_length = ""+ getFileLength(uploadFile.getAbsolutePath()) + content_disposition.getBytes().length+ content_type.getBytes().length;
// con.setRequestProperty("Content-Length", content_length);
// con.setRequestProperty("Cookie", cookie);
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);//-------
ds.writeBytes(content_disposition);
// LogUtils.e(content_disposition);
ds.writeBytes(content_type);
ds.writeBytes(end);
//---- 内容 -----
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
//----- 结束标记--
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary+"--" + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
// String responseMessage = con.getResponseMessage();
InputStream is = con.getInputStream();
StringBuffer b =new StringBuffer();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(is , "gbk"));
String str = null;
while((str= breader.readLine()) != null){
// LogUtil.e("-----"+str);
b.append(str);
// str = breader.readLine();
}
Object content = con.getContent();
/* 将Response显示于Dialog */
LogUtil.e("上传成功");
// showDialog("上传成功",context);
/* 关闭DataOutputStream */
ds.close();
String strFromServer = b.toString().trim();
String twoHyphens = "--";
String boundary = "-------------------------7de16713908a8";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设置传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
con.setRequestProperty("Accept-Encoding", "gzip,deflate");
con.setRequestProperty("Accept-Language", "zh-CN");
con.setRequestProperty("Cache-Control", "no-cache");
// con.setRequestProperty("uptype", "client");
String fileType = getFileType(uploadFile);
String newName = getFileName(uploadFile);
String content_disposition = "Content-Disposition: form-data; " +
"name=\"upfile\"; filename=\"" +
newName +".text" +"\"" + end;
String content_type = "Content-Type: text/plain"+end;
String content_length = ""+ getFileLength(uploadFile.getAbsolutePath()) + content_disposition.getBytes().length+ content_type.getBytes().length;
// con.setRequestProperty("Content-Length", content_length);
// con.setRequestProperty("Cookie", cookie);
/* 设置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);//-------
ds.writeBytes(content_disposition);
// LogUtils.e(content_disposition);
ds.writeBytes(content_type);
ds.writeBytes(end);
//---- 内容 -----
/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设置每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 从文件读取数据至缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将资料写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
//----- 结束标记--
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary+"--" + end);
/* close streams */
fStream.close();
ds.flush();
/* 取得Response内容 */
// String responseMessage = con.getResponseMessage();
InputStream is = con.getInputStream();
StringBuffer b =new StringBuffer();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(is , "gbk"));
String str = null;
while((str= breader.readLine()) != null){
// LogUtil.e("-----"+str);
b.append(str);
// str = breader.readLine();
}
Object content = con.getContent();
/* 将Response显示于Dialog */
LogUtil.e("上传成功");
// showDialog("上传成功",context);
/* 关闭DataOutputStream */
ds.close();
String strFromServer = b.toString().trim();
0 0
- java 表单上传
- 【零碎JAVA】表单文件上传
- android表单上传出现java.lang.NoClassDefFoundError
- java 模拟表单方式提交上传文件
- java客户端模拟表单上传文件
- java 模拟表单方式提交上传文件
- java客户端模拟表单上传文件
- java客户端模拟表单上传文件
- file表单上传,java后台接收案例
- java模拟form表单上传图片
- 多图上传--FormData表单&Java,SSM
- 表单上传
- 表单上传
- 上传表单
- 表单上传
- 上传表单
- java模拟post方式提交表单实现图片上传
- 使用FileUtils进行Java中Form表单上传文件
- 边缘计算
- 关于iOS多线程,你看我就够了(已更新)
- Flask-多对多关系
- 将tomcat源码导入eclipse
- json往后台传送富文本编辑器的内容时,丢失,变成null(空)
- java 表单上传
- ButterKnife的使用
- Maven常用命令
- 打鼹鼠
- Oracle 12.2中设置pdb随cdb启动而自动启动
- SQL语句DISTINCT
- AVX指令集矩阵乘向量算法
- 【上机笔试之八】二分法查找
- 用MySQL创建数据库和数据库表