java action上传文件_使用Java代码如何上传文件到Stuts Action中??

刚好写过直接上代码了

调用的地方的代码URL url = new URL(imageHttp);

BufferedImage img = ImageIO.read(url);

File file = new File(System.currentTimeMillis()+".jpg");

ImageIO.write(img, "jpg", file);

FileBody pic = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity();

StringBody content1 = new StringBody(content,Charset.forName("UTF-8"));

StringBody access_token = new StringBody(qq_access_token,Charset.forName("UTF-8"));

StringBody oauth_consumer_key = new StringBody(QQConnectConfig.getValue("app_ID").trim(),Charset.forName("UTF-8"));

StringBody openid = new StringBody(qq_open_id,Charset.forName("UTF-8"));

reqEntity.addPart("pic", pic);

reqEntity.addPart("access_token", access_token);

reqEntity.addPart("oauth_consumer_key", oauth_consumer_key);

reqEntity.addPart("openid", openid);

reqEntity.addPart("content", content1);

String res = HttpClientUtil.executeMultipartPOST(QQConnectConfig.getValue("addPicTURL"),reqEntity);

System.out.println("================="+res);

img.flush();

img=null;

工具类代码

/**

* Project Name:live_app

* Package Name:com.kuke.core.engine

* File Name:HttpClientUtil.java

* Create Time:2012-4-5 下午04:49:55

* Copyright (c) 2006 ~ 2012 Kuke Tech Dept All rights reserved.

*/

package com.kuke.core.util;

import java.io.IOException;

import java.util.List;

import org.apache.http.client.HttpClient;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.mime.MultipartEntity;

import org.apache.http.impl.client.BasicResponseHandler;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.HttpConnectionParams;

import org.apache.http.protocol.HTTP;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

*Class HttpClientUtil

*Descripton goes here

*@author:zhjt

*@version: Revision:2.0.0, 2012-4-5 下午04:49:55

*/

public final class HttpClientUtil

{

static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

static int CONNECTIONTIMEOUTMILLIS = 10000;

static int SOCKETTIMEOUTMILLIS = 10000;

/**

* 执行Http的返回结果

*/

public static String executeService(String url)throws Exception {

logger.debug("SSOService executeService start!");

logger.debug("url=" + url);

String result = "";

DefaultHttpClient httpclient = new DefaultHttpClient();

//设置超时事件

HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),CONNECTIONTIMEOUTMILLIS);

HttpConnectionParams.setSoTimeout(httpclient.getParams(), SOCKETTIMEOUTMILLIS);

try {

HttpGet httpget = new HttpGet(url);

ResponseHandler responseHandler = new BasicResponseHandler();

String responseBody = httpclient.execute(httpget, responseHandler);

result=responseBody;

logger.debug("SSOService executeService end!");

}catch(Exception e){

logger.debug("SSOService executeService error:"+e);

} finally {

httpclient.getConnectionManager().shutdown();

logger.debug("SSOService executeService shutdown!");

}

return result;

}

/**

* 执行Http的返回结果

*/

public static String executeServicePOST(String url, List nvps)

throws IOException {

logger.debug("SSOService executeServicePOST start!");

String result = "";

DefaultHttpClient httpclient = new DefaultHttpClient();

//设置超时事件

HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), CONNECTIONTIMEOUTMILLIS);

HttpConnectionParams.setSoTimeout(httpclient.getParams(), SOCKETTIMEOUTMILLIS);

try {

HttpPost httpost = new HttpPost( url + "?");

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

ResponseHandler responseHandler = new BasicResponseHandler();

String responseBody = httpclient.execute(httpost, responseHandler);

result=responseBody;

logger.debug("SSOService executeServicePOST end!");

}catch(Exception e){

logger.debug("SSOService executeServicePOST error:"+e);

} finally {

httpclient.getConnectionManager().shutdown();

logger.debug("SSOService executeServicePOST shutdown!");

}

return result;

}

public static String executeMultipartPOST(String url, MultipartEntity reqEntity) {

logger.debug(" executeMultipartPOST start!");

String result = "";

HttpClient httpclient = new DefaultHttpClient();

try {

HttpPost httppost = new HttpPost(url);

httppost.setEntity(reqEntity);

httppost.getRequestLine();

ResponseHandler responseHandler = new BasicResponseHandler();

String responseBody = httpclient.execute(httppost, responseHandler);

// HttpResponse response = httpclient.execute(httppost);

// HttpEntity resEntity = response.getEntity();

result=responseBody;

}catch(Exception e){

logger.debug("executeMultipartPOST error:"+e);

} finally {

httpclient.getConnectionManager().shutdown();

}

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值