java接口接收文件类txt、Excel 、、、、

关键字:注解:@RequestParam("file") MultipartFile file(接收文件)

/**
	 * 上传
	 * @param file
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @SuppressWarnings("unchecked")
    @RequestMapping("upload")
    public String uploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
    	
    	URL url;
    	HttpURLConnection con;
    	int state = -1;
    	
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        String urlStr = "";
        if (file.isEmpty()) {
            return "文件不能为空";
        }
        InputStream inputStream = file.getInputStream();
        List<List<String>> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename());
        inputStream.close();

        File f=new File("D:\\test\\a.txt");
        if (!f.exists()) {
            f.mkdirs();
        }
		FileOutputStream fos1=new FileOutputStream(f);
		OutputStreamWriter dos1=new OutputStreamWriter(fos1);
		File f2=new File("D:\\test\\b.txt");
		if (!f2.exists()) {
			f2.mkdirs();
		}
		FileOutputStream fos2=new FileOutputStream(f2);
		OutputStreamWriter dos2=new OutputStreamWriter(fos2);
		File f3=new File("D:\\test\\c.txt");
		if (!f3.exists()) {
			f3.mkdirs();
		}
		FileOutputStream fos3=new FileOutputStream(f3);
		OutputStreamWriter dos3=new OutputStreamWriter(fos3);
        for (int i = 1; i < list.size(); i++) {
            List<String> lo = list.get(i);
            //-------------------------start------------------------
            urlStr=lo.get(6);
            int counts = 0;
			   if (urlStr == null || urlStr.length() <= 0) {                       
			    return null;                 
			   }
			   while (counts < 5) {
			    try {
			     url = new URL(urlStr);
			     urlStr=i+"="+lo.get(1)+url;
			     con = (HttpURLConnection) url.openConnection();
			     state = con.getResponseCode();
			     System.out.println(counts +"= "+state);
			     if (state == 200) {
				      System.out.println("URL可用!");
				      dos1.write(urlStr+" ==>true"+"\n");
			     }
			     if (state == 403) {
			    	 dos3.write(urlStr+" ==>异常"+"\n");
			     }
			     if (state == 404 || state==410||state==301||state==302||state==304||state==400||state==401||state==500||state==501||state==502||state==503) {
			    	 System.out.println("URL不可用!");
			    	 dos2.write(urlStr+" ==>false"+"\n");
			     }
			     break;
			    }catch (Exception ex) {
			     counts++; 
			     System.out.println("URL不可用,连接第 "+counts+" 次");
			     if (counts==5) {
			    	 System.out.println(urlStr+" ==>异常");
						ex.printStackTrace();
						dos3.write(urlStr+" ==>异常"+"\n");
				}
			     continue;
			    }
			   }
            //---------------------------end-----------------------
        }
        dos1.close();
        dos2.close();
        dos3.close();
        return "download";
    }

 

package com.zkdj.urlCheck.spring_boot_1.main.java.service;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

@Service
public class UrlCheckService {
	/**
     * 处理上传的Excel文件
     *
     * @param in
     * @param fileName
     * @return
     * @throws Exception
     */
    public List getBankListByExcel(InputStream in, String fileName) throws Exception {
        List list = new ArrayList<>();
        //创建Excel工作薄
        Workbook work = this.getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }

            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
                    continue;
                }

                List<String> li = new ArrayList<>();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    String stringValueFromCell = getStringValueFromCell(cell);
                    li.add(stringValueFromCell);
                }
                list.add(li);
            }
        }
        work.close();
        return list;
    }

    /**
     * 判断文件格式
     *
     * @param inStr
     * @param fileName
     * @return
     * @throws Exception
     */
    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
        Workbook workbook = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(inStr);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(inStr);
        } else {
            throw new Exception("请上传excel文件!");
        }
        return workbook;
    }
    
    public static String getStringValueFromCell(Cell cell) { 
    	SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy"); 
    	DecimalFormat decimalFormat = new DecimalFormat("#.#"); 
    	String cellValue = ""; 
    	if(cell == null) { 
    		return cellValue; 
    		} else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { 
    			cellValue = cell.getStringCellValue(); 
    			} else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { 
    				if(HSSFDateUtil.isCellDateFormatted(cell)) { 
    					double d = cell.getNumericCellValue(); 
    					Date date = HSSFDateUtil.getJavaDate(d); 
    					cellValue = sFormat.format(date); 
    					} else { 
    						cellValue = decimalFormat.format((cell.getNumericCellValue())); 
    						} 
    				} else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { 
    					cellValue = ""; 
    					} else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { 
    						cellValue = String.valueOf(cell.getBooleanCellValue()); 
    						} else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) { 
    							cellValue = ""; 
    							} else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { 
    								cellValue = cell.getCellFormula().toString(); 
    								} 
    	return cellValue; 
    	}
}

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值