java后台post方法上传文件

java后台post方法上传文件,第1张

/** * 执行post请求并将返回内容转为json格式返回 */public static JsonObject doPost(String url, JsonObject message)throws WeiXinException {JsonObject jo = nullPrintWriter out = nullInputStream in = nulltry {if (url.startsWith("https")){//https方式提交需要SSLContext sc = SSLContext.getInstance("SSL")sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },new java.security.SecureRandom())URL console = new URL(url)HttpsURLConnection conn = (HttpsURLConnection) console.openConnection()conn.setSSLSocketFactory(sc.getSocketFactory())conn.setHostnameVerifier(new TrustAnyHostnameVerifier())conn.connect()in = conn.getInputStream()}else{in = new URL(url).openStream()}// 打开和URL之间的连接URLConnection conn = new URL(url).openConnection()// 设置通用的请求属性conn.setRequestProperty("accept", "*/*")conn.setRequestProperty("connection", "Keep-Alive")conn.setRequestProperty("user-agent","Mozilla/4.0 (compatibleMSIE 6.0Windows NT 5.1SV1)")// 发送POST请求必须设置如下两行conn.setDoOutput(true)conn.setDoInput(true)// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream())// 发送请求参数out.print(message.toString())// flush输出流的缓冲out.flush()// POST请求out.flush()out.close()in = conn.getInputStream()jo = JSON.parse(getContext(in))doExeption(jo)} catch (MalformedURLException e) {e.printStackTrace()} catch (ProtocolException e) {e.printStackTrace()} catch (IOException e) {e.printStackTrace()} catch (KeyManagementException e) {e.printStackTrace()} catch (NoSuchAlgorithmException e) {e.printStackTrace()} finally {if (out != null) {out.flush()out.close()}if (in != null ){try {in.close()} catch (IOException e) {e.printStackTrace()}}}return jo}

/**

     * 向指定 URL 发送POST方法的请求

     * 

     * @param url

     *            发送请求的 URL

     * @param param

     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。

     * @return 所代表远程资源的响应结果

     */

    public static String sendPost(String url, String param) {

        PrintWriter out = null

        BufferedReader in = null

        String result = ""

        try {

            URL realUrl = new URL(url)

            // 打开和URL之间的连接

            URLConnection conn = realUrl.openConnection()

            // 设置通用的请求属性

            conn.setRequestProperty("accept", "*/*")

            conn.setRequestProperty("connection", "Keep-Alive")

            conn.setRequestProperty("user-agent",

                    "Mozilla/4.0 (compatible MSIE 6.0 Windows NT 5.1SV1)")

            // 发送POST请求必须设置如下两行

            conn.setDoOutput(true)

            conn.setDoInput(true)

            // 获取URLConnection对象对应的输出流

            out = new PrintWriter(conn.getOutputStream())

            // 发送请求参数

            out.print(param)

            // flush输出流的缓冲

            out.flush()

            // 定义BufferedReader输入流来读取URL的响应

            in = new BufferedReader(

                    new InputStreamReader(conn.getInputStream()))

            String line

            while ((line = in.readLine()) != null) {

                result += line

            }

        } catch (Exception e) {

            System.out.println("发送 POST 请求出现异常!"+e)

            e.printStackTrace()

        }

        //使用finally块来关闭输出流、输入流

        finally{

            try{

                if(out!=null){

                    out.close()

                }

                if(in!=null){

                    in.close()

                }

            }

            catch(IOException ex){

                ex.printStackTrace()

            }

        }

        return result

    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11685875.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存