一于 2014-03-13 14:35
浏览 154
已采纳

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

使用Java代码如何上传文件到Stuts Action中??
求大神。。。。。

  • 写回答

5条回答 默认 最新

  • tianchao_ 2014-03-13 17:59
    关注

    刚好写过直接上代码了

    调用的地方的代码
    [code="java"]
    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;
    

    [/code]

    工具类代码
    [code="java"]
    /**

    • 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的返回结果<Get>
     */
    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<String> 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的返回结果<POST>
     */
    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<String> 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<String> 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;
    }
    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?