在后台得到一个Excel文件的路径,然后用使用BufferedInputStream和BufferOutputStream将文件保存到服务器,然后读取文件中的数据并将它们保存到数据库中。但是问题出现了:每次都复制了,但是在判断BufferedInputStream的read方法的时候,read的结果都是-1.而且被复制的原文件中也变成了空的。实在是焦头烂额啊,哪位大侠指点一下。下面是代码:
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
} public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
System.out.println("buffer.length:"+buffer.length);
System.out.println("____________赋值文件:"+in.available()+"字节"); //这里每次都是-1
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
// 将缓冲区中的数据全部写出
// out.flush();
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String doExcel() {
String str = this.getRequest().getParameter("filepath");
try {
fileName = URLDecoder.decode(URLDecoder.decode(str, "utf-8"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.myFile = new File(fileName);
imageFileName = new Date().getTime() + ".xls";
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/Excel" ) + " / " + imageFileName);
copy(myFile, imageFile);
setMessage("成功!");
return "successMessage";
}
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
} public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
System.out.println("buffer.length:"+buffer.length);
System.out.println("____________赋值文件:"+in.available()+"字节"); //这里每次都是-1
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
// 将缓冲区中的数据全部写出
// out.flush();
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String doExcel() {
String str = this.getRequest().getParameter("filepath");
try {
fileName = URLDecoder.decode(URLDecoder.decode(str, "utf-8"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.myFile = new File(fileName);
imageFileName = new Date().getTime() + ".xls";
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/Excel" ) + " / " + imageFileName);
copy(myFile, imageFile);
setMessage("成功!");
return "successMessage";
}
out = new BufferedOutputStream( new FileOutputStream(src), BUFFER_SIZE); //这里有问题吧,源文件和目标文件是同一个,改成new FileOutputStream(dst)
LZ就没看我的回复?你的代码有问题,输入输出都是同一个文件,当你new FileOutputStream(src),你的源文件就被清空了,当然read返回的结果只能是-1了
这是怎么回事?1303268630406.xls这个文件没有复制到位?
改成getRealPath( "/Excel" ) + "/" + imageFileName试试看
C:\Program Files\PlantixStudio1.6\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\statistics\Excel