Txt文件上传
以及判断txt的编码格式
展示中文
解析txt格式不对有时会乱码 解析默认是utf-8
新建的txt文件默认是"GBK"格式的
public OutSimpleUploadFile importPwdRule(MultipartFile uploadFile) throws IOException
{
//拿取前端传来的文件
byte[] b = uploadFile.getBytes();
String data1 = new String(b, StandardCharsets.UTF_8);
String data2 = new String(b, "GBK");
String fileName = uploadFile.getOriginalFilename();
//判断文件编码格式
String[] datas;
if (data1.length() < data2.length())
{
datas = data1.split("\r\n");
}
else
{
datas = data2.split("\r\n");
}
List<String> dataList = new ArrayList<>();
int k = 0;
//限制行数
if (k < 3000)
{
for (String s :datas)
{
dataList.add(s);
}
k++;
}
//去重
for (int i = 0; i < dataList.size() - 1; i++)
{
for (int j = dataList.size() - 1; j > i; j--)
{
if (dataList.get(j).toString().equals(dataList.get(i).toString()))
{
dataList.remove(j);
}
}
}
StringBuffer sb = new StringBuffer();
for (String s : dataList)
{
sb.append(s);
sb.append("\r\n");
}
int number = dataList.size();
OutSimpleUploadFile outSimpleUploadFile = new OutSimpleUploadFile();
String sb1 = sb.substring(0, sb.length() - 1);
outSimpleUploadFile.setSimple(sb);
outSimpleUploadFile.setNumber(number);
//返回解析后的文本内容
return outSimpleUploadFile;
}
Txt文件下载
代码及效果图
实现浏览器下载必加代码
response.setContentType(“application/octet-stream”);
response.setHeader(“content-disposition”, “attachment;filename=” + URLEncoder.encode(文件名, “UTF-8”));
response.setContentType("application/octet-stream");
//处理文件名
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
String fileName = "随便写" +".txt";
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//从数据库中拿取信息
List<T> datas = .....
byte[] data = datas.getData();
if (data == null)
{
throw new Exception("导出失败");
}
BufferedInputStream bis = null;
OutputStream os = null;
try
{
os = response.getOutputStream();
os.write(data);
os.flush();
} catch (Exception e)
{
log.error("请求失败" + e.getMessage(), e);
throw new Exception("请求失败");
} finally
{
try
{
if (bis != null)
{
bis.close();
}
} catch (IOException e)
{
log.error("", e);
}
}
2.通过拿取文件绝对路径来实现文件下载
这位大佬写的就可以
[https://blog.csdn.net/eieiei438/article/details/94013729]
(https://blog.csdn.net/eieiei438/article/details/94013729)
根据图片(“下载路径”)下载到本地
例如
https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1591033009,497553808&fm=26&gp=0.jpg
public void saveUrlAs(String imgUrl, String filePath){
try {
// 构造URL
URL url = new URL(imgUrl);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File file = new File(imgUrl);
String jname = file.getFilaName();
OutputStream os = new FileOutputStream("D:\\pig/"+jname);//保存路径
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//创建不同的文件夹目录
File file=new File(filePath);
//判断文件夹是否存在
if (!file.exists())
{
//如果文件夹不存在,则创建新的的文件夹
file.mkdirs();
}
}
Excel表格的拼接及下载
//下载
public int download() throws Exception
{
//1.获取需要放到Excel表中的数据
List<CgbV> cgbVS = cgbMapper.queryAll();
System.out.println(cgbVS);
//新建Excel表
HSSFWorkbook excel = new HSSFWorkbook();
//添加一个sheet(工作簿)
HSSFSheet hssfSheet = excel.createSheet("购物清单");
//创建第一行
HSSFRow row = hssfSheet.createRow(0);
//给第一行第一个单元格赋值
row.createCell(0).setCellValue("种类名称");
row.createCell(1).setCellValue("采购名称");
row.createCell(2).setCellValue("数量");
row.createCell(3).setCellValue("单位");
row.createCell(4).setCellValue("单价");
row.createCell(5).setCellValue("总价");
HSSFRow row1;
for (int i = 1; i<cgbVS.size()+1 ; i++){
row1 = hssfSheet.createRow(i);
//给第一行第一个单元格赋值
row1.createCell(0).setCellValue(cgbVS.get(i-1).getName());
row1.createCell(1).setCellValue(cgbVS.get(i-1).getCgName());
row1.createCell(2).setCellValue(cgbVS.get(i-1).getCgSl());
row1.createCell(3).setCellValue(cgbVS.get(i-1).getDw());
row1.createCell(4).setCellValue(String.valueOf(cgbVS.get(i-1).getPrice()));
row1.createCell(5).setCellValue(cgbVS.get(i-1).getZj());
}
//如果最后一个需要一个汇总 算总价
Double count = cgbMapper.count();
HSSFRow row2 = hssfSheet.createRow(cgbVS.size()+1);
row2.createCell(0).setCellValue("总价");
row2.createCell(5).setCellValue(count);
try
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String date = sdf.format(new Date());
FileOutputStream fout = new FileOutputStream("D:\\桌面\\XXX_"+date+".xls");
//导出
excel.write(fout);
fout.close();
} catch (FileNotFoundException e){
e.printStackTrace();
}
return 1;
}
注 : 并非都是楼主自己写的
未完待续…