尽管给出了预期的输出,但是以下代码的执行无法终止,为什么?

时间:2015-07-16 20:40:47

标签: java execution terminate

执行以下代码,用于更新excel中存在的URL的重定向URL,尽管给出了预期的输出,但无法终止。程序选择存在要检查的URLS的Excel文件,然后更新excel中存在的URL重定向到的URL。任何人都可以给我理由。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Selenium_sample1 extends JFrame {

    public static void main(String[] args) throws IOException {
        String s, sss = null; int rownum = 6;
        JFrame Jf = new JFrame();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home") + "/Desktop"));
        int result = fileChooser.showOpenDialog(Jf);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            sss = selectedFile.getAbsolutePath();
            System.out.println("Selected file: " + sss);
        } 

        FileInputStream fileI = new FileInputStream(new File(sss));

        // Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(fileI);

        // Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);
        Cell cell = null;
        Row row = null;
        int lastr = sheet.getLastRowNum();
        System.out.println(lastr);
        List <String> RedirectedUrl = new ArrayList<String>();
        for (int i = 0; i <= lastr; i++) {
            row = sheet.getRow(i);
            // For each row, iterate through all the columns
            cell = row.getCell(0);
            System.out.println(cell.toString());
            URL url = new URL(cell.toString());
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setInstanceFollowRedirects(false);
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            connection.connect();
            int code = connection.getResponseCode();
            // e.printStackTrace();

            System.out.println(code);
            s = connection.getHeaderField("Location");
            if (code / 100 == 3)
                RedirectedUrl.add(s);
            else 
                RedirectedUrl.add(" ");            
        }
        for (String ss: RedirectedUrl) {                
            Row roww = sheet.createRow(rownum);
            Cell celll = roww.createCell(4);
            celll.setCellValue(ss);
            rownum = rownum + 1;
        }
        fileI.close();
        FileOutputStream fos = new FileOutputStream(new File(sss));
        workbook.write(fos); 
        fos.close();
        System.out.println("Task Completed");                 
    }                
}

0 个答案:

没有答案