晚上好,今天我在使用 Print Writer 将文件上传到服务器时遇到了一个奇怪的情况,文件被传输了,我尝试FileOutPutStream
改用它并解决了问题,我的问题是为什么PrintWriter
会出现这种奇怪的行为,这是我的代码用于上传文件并将其保存在服务器上:
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
int i;
if(request instanceof MultipartWrapper){
String DestinationPath="C:\\";
MultipartWrapper request1=(MultipartWrapper)request;
File f=request1.getFile("photo");
java.io.FileInputStream fis=new java.io.FileInputStream(f);
//PrintWriter out=new PrintWriter(DestinationPath+f.getName()); causes the problem mentioned above
java.io.FileOutputStream out=new java.io.FileOutputStream(DestinationPath+f.getName());
while((i=fis.read())!=-1){
out.write(i);
}
fis.close();
out.close();
}
}