String path = new FileTxtHandleUtil().projectSource(EXCEL_TEMPLATE_PATH2); FileInputStream fileInputStream = new FileInputStream(path); Workbook workbook = ExcelTemplateUtil.buildByTemplate(fileInputStream, staticSource, dynamicSourceList); // 2.保存到本地 OutputStream outputStream = response.getOutputStream(); response.reset(); String fileName = URLEncoder.encode("白名单正确数据导出.xlsx", "UTF-8"); response.addHeader("Content-Length", resultList.size()+""); response.setHeader("Content-disposition", "attachment; filename=" + fileName); response.setContentType("Content-Type:application/vnd.ms-excel");response.addHeader("Content-Length", resultList.size()+"");怎么设置文件大小比较好
时间: 2024-02-08 15:03:12 浏览: 101
文件大小可以通过获取文件的字节数来设置。你可以在代码中使用以下方法获取文件的字节数:
```
long fileSize = new File(filePath).length();
```
然后,你可以将文件大小设置到响应头中:
```
response.setHeader("Content-Length", String.valueOf(fileSize));
```
这样就可以将文件大小设置到响应头中了。
相关问题
inventoryButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (isFastClick()) { showLoadToast(); goToMainActivity(); loadExcelFile(); } } private boolean isFastClick() { return ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext()); } private void showLoadToast() { CustomToast.showLoad(HomeActivity.this, getString(R.string.load)); } private void goToMainActivity() { Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } private void loadExcelFile() { new AsyncTask<Void, Void, List<String>>() { @Override protected List<String> doInBackground(Void... voids) { return readExcelFile(); } @Override protected void onPostExecute(List<String> data) { showDataInListView(data); } }.execute(); } private void showDataInListView(List<String> data) { listView = findViewById(R.id.rv_list); if (listView != null) { ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); } } }); private List<String> readExcelFile() { List<String> datas = new ArrayList<>(); try (FileInputStream inputStream = new FileInputStream("/sdcard/Template/IC1001.xlsx"); Workbook workbook = WorkbookFactory.create(inputStream)) { Sheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); datas.add(cell.toString()); } } } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } return datas; }如何优化,举例说明
这段代码可以进行如下优化:
1. 使用 Lambda 表达式替代匿名内部类,使代码更加简洁。
2. 在读取 Excel 文件时,可以使用 BufferedInputStream 和 BufferedOutputStream 进行缓冲,提高读写效率。
3. 在读取 Excel 文件时,可以将文件路径作为参数传递进来,而不是在代码中写死,提高代码的灵活性。
4. 在读取 Excel 文件时,可以使用 try-with-resources 语句自动关闭流,避免资源泄漏。
5. 在读取 Excel 文件时,可以使用 Apache POI 提供的 CellType 枚举类型判断单元格类型,避免因类型转换错误导致程序异常。
修改后的代码如下:
```
inventoryButton.setOnClickListener(v -> {
if (isFastClick()) {
showLoadToast();
goToMainActivity();
loadExcelFile("/sdcard/Template/IC1001.xlsx");
}
});
private void loadExcelFile(String filePath) {
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... voids) {
return readExcelFile(filePath);
}
@Override
protected void onPostExecute(List<String> data) {
showDataInListView(data);
}
}.execute();
}
private List<String> readExcelFile(String filePath) {
List<String> datas = new ArrayList<>();
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellTypeEnum() == CellType.STRING) {
datas.add(cell.getStringCellValue());
} else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
datas.add(Double.toString(cell.getNumericCellValue()));
}
}
}
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}
return datas;
}
```
inventoryButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (isFastClick()) { showLoadToast(); goToMainActivity(); loadExcelFile(); } } private boolean isFastClick() { return ButtonOnCilk.isFastViewClick(inventoryButton, getBaseContext()); } private void showLoadToast() { CustomToast.showLoad(HomeActivity.this, getString(R.string.load)); } private void goToMainActivity() { Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); } private void loadExcelFile() { new AsyncTask<Void, Void, List<String>>() { @Override protected List<String> doInBackground(Void... voids) { return readExcelFile(); } @Override protected void onPostExecute(List<String> data) { showDataInListView(data); } }.execute(); } private void showDataInListView(List<String> data) { listView = findViewById(R.id.rv_list); if (listView != null) { ArrayAdapter<String> adapter = new ArrayAdapter<>(HomeActivity.this, android.R.layout.simple_list_item_1, data); listView.setAdapter(adapter); } } });private List<String> readExcelFile() { List<String> datas = new ArrayList<>(); try { FileInputStream inputStream = new FileInputStream("/sdcard/Template/IC1001.xlsx"); Workbook workbook = WorkbookFactory.create(inputStream); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { datas.add(cell.toString()); } } workbook.close(); } catch (IOException | InvalidFormatException e) { e.printStackTrace(); } return datas; }报错
根据给出的代码和报错信息,很有可能是因为没有添加读取外部存储权限所导致的,你可以在 AndroidManifest.xml 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
同时,你也需要在运行时动态请求该权限。可以使用以下代码请求权限:
```java
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
```
如果仍然无法解决问题,请将完整的报错信息提供给我,以便更好地定位问题。
阅读全文