--JRuby #open génère une erreur dans l'environnement Windows 10
--Argument --url (nom de variable: lien)
JavaOpen.java
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class JavaOpen{
    public static String open(String link, int time_limit){
        String html = "";
        try {
            URL url = new URL(link);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(time_limit*300);
            con.setReadTimeout(time_limit*700);
            try (InputStream is = con.getInputStream();){
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteChunk = new byte[8192];
                int n;
                while ( (n = is.read(byteChunk)) > 0 ) {
                    baos.write(byteChunk, 0, n);
                }
                byte[] bytes = baos.toByteArray();
                html = bytesToHtml(bytes);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        } finally {
            return html;
        }
    }
    public static String bytesToHtml(byte[] src) throws UnsupportedEncodingException {
        String[] char_codes = { "UTF8","SJIS","EUC_JP","EUC_JP_LINUX","EUC_JP_Solaris" };
        for (String cc: char_codes){
            String s_tmp = new String(src, cc);
            byte[] b_tmp = s_tmp.getBytes(cc);
            if (Arrays.equals(src, b_tmp)) {
                return s_tmp;
            }
        }
        return "";
    }
}
--String [] char_codes = {"UTF8", "SJIS", "EUC_JP", "EUC_JP_LINUX", "EUC_JP_Solaris"}; ʻest un code de caractère auquel vous pourrez peut-être accéder, alors n'hésitez pas à l'utiliser.  --Je voulais trouver une alternative pour le nom de variable time_limit ...  -- setConnectTimeout: setReadTimeout` = 3: 7 Je l'ai alloué, mais qu'en est-il normal?
――Je voulais savoir lire à la fois au lieu de 8192 octets à la fois, mais j'ai perdu l'intention de bouger.
Recommended Posts