java读取文件中的图片上传_读取Java中文件上传的内容 - java

我知道如何上传文件:

File Uploading Form

enctype="multipart/form-data">

File :

这是读取文件的类:

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class BufferedReaderExample {

public static void main(String[] args) {

try (BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt")))

{

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {

System.out.println(sCurrentLine);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

现在,您如何将它们链接在一起。用户将文件上传到服务器上,然后服务器对其进行处理。即,它接受文件并打印其内容。我可以为此使用struts框架吗?我想上传Excel工作表并将内容打印到jsp页面。我有使用apache poi在Java中读取Excel工作表的代码。但是读取excel文件的路径是硬编码的。您如何从上传的文件中获取它?

这是用于读取Excel工作表的文件:

public class ReadExcelDemo

{

public static void main(String[] args)

{

try

{

FileInputStream file = new FileInputStream(new File("howtodoinjava_demo.xlsx"));

//Create Workbook instance holding reference to .xlsx file

XSSFWorkbook workbook = new XSSFWorkbook(file);

//Get first/desired sheet from the workbook

XSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows one by one

Iterator rowIterator = sheet.iterator();

while (rowIterator.hasNext())

{

Row row = rowIterator.next();

//For each row, iterate through all the columns

Iterator cellIterator = row.cellIterator();

while (cellIterator.hasNext())

{

Cell cell = cellIterator.next();

//Check the cell type and format accordingly

switch (cell.getCellType())

{

case Cell.CELL_TYPE_NUMERIC:

System.out.print(cell.getNumericCellValue() + "t");

break;

case Cell.CELL_TYPE_STRING:

System.out.print(cell.getStringCellValue() + "t");

break;

}

}

System.out.println("");

}

file.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

输出:

ID NAME LASTNAME

1.0阿米特·舒克拉

2.0 Lokesh古普塔

3.0 John Adwards

4.0布莱恩·舒尔茨

但是,如何将上传的文件和servlet文件缝合在一起。您如何阅读上传的文件?

参考方案

您可以通过将整个过程分为两个步骤来实现

1)上传文件

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

if (!isMultipart) {

return;

}

// Create a factory for disk-based file items

DiskFileItemFactory factory = new DiskFileItemFactory();

// Sets the size threshold beyond which files are written directly to

// disk.

factory.setSizeThreshold(MAX_MEMORY_SIZE);

// Sets the directory used to temporarily store files that are larger

// than the configured size threshold. We use temporary directory for

// java

factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

// constructs the folder where uploaded file will be stored

String uploadFolder = getServletContext().getRealPath("")

+ File.separator + DATA_DIRECTORY;

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint

upload.setSizeMax(MAX_REQUEST_SIZE);

try {

// Parse the request

List items = upload.parseRequest(request);

Iterator iter = items.iterator();

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (!item.isFormField()) {

String fileName = new File(item.getName()).getName();

String filePath = uploadFolder + File.separator + fileName;

File uploadedFile = new File(filePath);

System.out.println(filePath);

// saves the file to upload directory

item.write(uploadedFile);

}

}

// displays done.jsp page after upload finished

getServletContext().getRequestDispatcher("/done.jsp").forward(

request, response);

} catch (FileUploadException ex) {

throw new ServletException(ex);

} catch (Exception ex) {

throw new ServletException(ex);

}

2)上传后,将文件位置传递到使用apche poi读取文件数据的方法。使用File uploadedFile = new File(filePath);对象获取文件位置。

Java Scanner读取文件的奇怪行为 - java

因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…Java:从文件系统加载资源 - java

我的项目设定我有以下项目设置:\program.jar \images\logo.png 在我的代码中,我使用相对URL "images/logo.png"引用图像。问题如果我在目录中使用以下命令运行此程序:c:\projects\program_dir\bin\>java -jar program.jar 然后一切正常,Java能…对于Java中的isDirectory和isFile,文件始终返回false - java

为什么file为isFile()方法返回false,即使它是file。当它是目录时,它为isDirectory()返回false。难道我做错了什么?我测试的这些文件/目录不存在,我需要创建它们,所以这就是为什么我要测试使用createFile()还是mkdir()的原因。File file = new File("C:/Users/John/Des…Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…Java Globbing模式以匹配目录和文件 - java

我正在使用递归函数遍历根目录下的文件。我只想提取*.txt文件,但不想排除目录。现在,我的代码如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是这样做将不会匹配任何目录,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值