无法获取读取.xls文件的路径

时间:2012-06-26 06:43:29

标签: java jsp servlets

我正在开发一个.xls读者,他将.xls文件读取并读取,然后将输出作为纯文本 但我面临一些问题,以获取文件路径进入servlet ...我正在使用poi.jar来读取.xls文件..

.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Upload Form</title>

    </head>


    <body bgcolor="lightblue">
 <table id='table1' align="center" >
                <tr>

                </tr></table><br/><br/><br/>  
            <br/>

            <table border="1" width="50%" bgcolor="#C2DFFF">
                <tr>
                    <td width="100%">
                        <form action="writeExcel" method="post" enctype="multipart/form-data">

                            <h2 align="center">Welcome</h2>
  <center>

      <input type="file" name="photo"/>
                                <input type="file" name="hem"/>
                                <br/><br/>
                                <input type="submit" value="Save"/>
                                &nbsp;
                                <input type="button" value="Logout" name="logout" onclick="goToURL();" />
                                <br/>
                                <br/>

                            </center>     
                        </form>
                    </td>
                </tr>
</table>

            <br/><br/>


    </body>
</html>

的.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class writeExcel extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String fileName = request.getParameter("hem");



       Vector dataHolder = ReadCSV(fileName);
        printCellDataToConsole(dataHolder);
    }

   public static Vector ReadCSV(String fileName) {
        Vector cellVectorHolder = new Vector();

        try {
            FileInputStream myInput = new FileInputStream(fileName);

            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            Iterator rowIter = mySheet.rowIterator();

            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator cellIter = myRow.cellIterator();
                Vector cellStoreVector = new Vector();
                while (cellIter.hasNext()) {
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    cellStoreVector.addElement(myCell);
                }
                cellVectorHolder.addElement(cellStoreVector);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cellVectorHolder;
    }

    private static void printCellDataToConsole(Vector dataHolder) {

        for (int i = 0; i < dataHolder.size(); i++) {
            Vector cellStoreVector = (Vector) dataHolder.elementAt(i);
            for (int j = 0; j < cellStoreVector.size(); j++) {
                HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
                String stringCellValue = myCell.toString();
                System.out.print(stringCellValue + "\t");
            }
            System.out.println();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

@AVD:fileName是否代表文件/ doc的绝对路径?如果没有则设置/获得实际(绝对)路径。

@Nitin Sharma:谢谢AVD ..当我使用finename =&#34; C:\ a.xls&#34;工作正常但我试图从.jsp到文件的路径,它只提供文件名不完整路径

@AVD:您必须将上传的文件保存在您的web-app根目录(或子文件夹)下,或使用上传文件的InputStream(Servlet 3.0 Part或Apache fileupload api)对象而不是fileName。

@Nitin Sharma:我不想上传文件...我只想获得该文件的完整路径..

@AVD:没有什么可说的!没有选择。 (不要保存上传的文件而是使用上传文件的流)。

@Nitin Sharma:我使用过但是poi使用File类对象来读取文件..但是Apache fileupload api使用FileItem类对象来读取或获取.xls文件的路径...而且我无法类型转换FileItem到文件


看一下POIFSFileSystem类,它需要InputStream参数,你可以通过FileItem.getInputStream()(Commons upload)获得。

POIFSFileSystem myFileSystem = new POIFSFileSystem(fileItem.getInputStream());