如何在struts2中结合HttpClient进行文件上传
来源:互联网 发布:北京市劲松职高网络 编辑:程序博客网 时间:2024/05/17 13:43
最近遇到了用httpclient进行上传文件的问题,下面我就和大家简单的说一下:
package com.imps.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TabinfoAction extends BaseAction
{
private File[] myFile;
private String[] myFileFileName;// 文件名
private Integer[] myFileFileSize;// 文件大小
private String[] myFileContentType;// 文件类型
public String uploadPost()
{
String posturl ="http://127.0.0.1:8080/settabimage.aspx";
System.out.println(posturl);
String status=null;
for(int i=0;i<myFile.length;i++)
{
FileInputStream file=new FileInputStream(myFile[i]);
InputStream in = new BufferedInputStream(file);
PostMethod post=new PostMethod(posturl);
post.setRequestBody(in);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response=new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
post.releaseConnection();
in.close();
file.close();
}
}
public File[] getMyFile() {
return myFile;
}
public void setMyFile(File[] myFile) {
this.myFile = myFile;
}
public String[] getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}
public Integer[] getMyFileFileSize() {
return myFileFileSize;
}
public void setMyFileFileSize(Integer[] myFileFileSize) {
this.myFileFileSize = myFileFileSize;
}
public String[] getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}package com.imps.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TabinfoAction extends BaseAction
{
private File[] myFile;
private String[] myFileFileName;// 文件名
private Integer[] myFileFileSize;// 文件大小
private String[] myFileContentType;// 文件类型
public String uploadPost()
{
String posturl ="http://127.0.0.1:8080/settabimage.aspx";
System.out.println(posturl);
String status=null;
for(int i=0;i<myFile.length;i++)
{
FileInputStream file=new FileInputStream(myFile[i]);
InputStream in = new BufferedInputStream(file);
PostMethod post=new PostMethod(posturl);
post.setRequestBody(in);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response=new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
post.releaseConnection();
in.close();
file.close();
}
}
public File[] getMyFile() {
return myFile;
}
public void setMyFile(File[] myFile) {
this.myFile = myFile;
}
public String[] getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}
public Integer[] getMyFileFileSize() {
return myFileFileSize;
}
public void setMyFileFileSize(Integer[] myFileFileSize) {
this.myFileFileSize = myFileFileSize;
}
public String[] getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
千万记住不要记忘记关闭流和释放http连接
- 如何在struts2中结合HttpClient进行文件上传
- 如何在struts2中结合HttpClient进行文件上传
- 如何在Struts中进行文件上传
- HttpClient结合Structs2进行文件上传(Action返回字符串)
- 如何在struts2中上传和下载文件
- 如何在Struts中进行文件上传[转载]
- httpClient上传文件 在Spring MVC中解析文件
- 在Struts2中实现文件上传
- 在Struts2中实现文件上传
- 在Struts2中实现文件上传
- 在Struts2中实现文件上传
- 关于struts2框架工程HttpClient上传文件
- struts2 如何 上传文件
- 利用HttpClient进行http文件上传
- 利用HttpClient进行http文件上传
- 利用HttpClient进行http文件上传
- 利用HttpClient进行http文件上传
- 使用 HttpClient 4 进行文件上传
- 与程序员说说心里话(转载)
- Linux cgroup机制分析之cpuset subsystem 【转】
- 7条成功守则(转载)
- mysql的wait_timeout 的原因,有可能c3p0的连接失败,造成请求失败
- 一直简单拖垮服务器的办法
- 如何在struts2中结合HttpClient进行文件上传
- 游戏人工智能--FSM篇
- mssql学习笔记【1】
- 懂得坚持-------年轻人不能犯的毛病
- 网页代码的优化
- A Standard for Enterprise Project Management
- Performance Analysis of Queuing and Computer Networks
- 数据库范式:掀起你的盖头来(1)
- 3ds Max 2009 Bible