服务器上有一个servlet叫ReadFileDataService:作用是通过request传递要访问的文件名,然后通过I/O流把文件通过response返回给客户端,这个servlet的代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {//获取用户访问这个servlet的URL
String  requestURL= request.getRequestURL().toString();
//获取用户想读取的文件名
String filename=requestURL.substring(requestURL.lastIndexOf("/")+1);//前置机相关文件存放的路径
String filepath = "F:\\test\\";File file = new File(filepath + filename);//读取文件的缓冲区
byte[] buff = new byte[1024];
//读取文件
FileInputStream is = null;
//只有文件存在并且可读写才返回给用户
if(file.exists() && file.canRead() && file.canWrite())
{
try {
//response.setCharacterEncoding("GBK");
//response.setContentType("application/vnd.ms-excel;charset=GBK");
is = new FileInputStream(file);
OutputStream ou = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(ou);
while((bis.read(buff))!=-1)
{
bos.write(buff);
}
bos.flush();
bos.close();
bis.close();} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else throw new FileNotFoundException("访问的文件不存在");客户机上有个File readFile(String path):作用是调用以上的代码,然后返回所求文件,代码如下:
public File readFile(String path) throws IOException  {
// 从path中获取文件名
String filename = path.substring(path.lastIndexOf("/") + 1);
File file = new File(tempFilePath + filename);
HttpURLConnection huc = null;
byte[] buff = new byte[1024];
// 建立http连接
huc = httpConnection(path);if(huc.getInputStream().available()!=0)
{
try {
if(!file.exists())
{
file.createNewFile();
}
BufferedInputStream bis = new BufferedInputStream(huc.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
while(bis.read(buff)!=-1)
{
bos.write(buff);
}
bos.flush();
bis.close();
bos.close();
}
 catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else throw new FileNotFoundException("要访问的文件不存在!");return file;
}能正常传递*.txt文件,而传递内容少的excel表格的话也没问题,然后现在存在严重的问题是:当excel内容较多时,就会有异常,文件大小会变,打开时会弹出这个窗口
所以向各位老师请教请教,望能为学生传道授业解惑,谢谢了!!JavaExcel