当前位置: 技术问答>java相关
这个上传Bean有问题么?
来源: 互联网 发布时间:2015-07-06
本文导语: 这个上传Bean怎么本地的文件可以上传(服务器),但是在另外的机器(客户机)上 传出错!提示: 文件上传失败,该文件不存在! 下面是:Bean的原码,帮一把!谢谢!!! import javax.servlet.http.HttpServletRequest; import javax...
这个上传Bean怎么本地的文件可以上传(服务器),但是在另外的机器(客户机)上
传出错!提示:
文件上传失败,该文件不存在!
下面是:Bean的原码,帮一把!谢谢!!!
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.*;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.*;
public class FileUploadBean
{
private String savePath, filepath, filename, contentType, saveFileName;
private Dictionary fields;
public String getFilename()
{
return filename;
}
public String getSaveFileName()
{
return saveFileName;
}
public String getFilepath()
{
return filepath;
}
public void setSavePath(String savePath)
{
this.savePath = savePath;
}
public String getContentType()
{
return contentType;
}
public String getFieldValue(String fieldName)
{
if (fields == null || fieldName == null)
return null;
return (String) fields.get(fieldName);
}
private void setFilename(String s)
{
if (s==null)
return;
int pos = s.indexOf("filename="");
if (pos != -1)
{
filepath = s.substring(pos+10, s.length()-1);
//System.out.println("filepath: " + filepath);
File f = new File(filepath);
if (f.exists())
{
//System.out.println("filepath exist: " + filepath);
}
else
{
filepath = "";
//System.out.println("filepath not exist: " + filepath);
}
// Windowsä¯ÀÀÆ÷·¢ËÍÍêÕûµÄÎļþ·¾¶ºÍÃû×Ö,µ«Linux/UnixºÍMacä¯ÀÀÆ÷Ö»·¢ËÍÎļþÃû×Ö
pos = filepath.lastIndexOf("\");
if(pos != -1) filename = filepath.substring(pos + 1);
else filename = filepath;
//System.out.println("filename: " + filename);
}
}
private void setContentType(String s)
{
if (s==null)
return;
int pos = s.indexOf(": ");
if(pos != -1) contentType = s.substring(pos+2, s.length());
}
public void doUpload(HttpServletRequest request) throws IOException
{
ServletInputStream in = request.getInputStream();
byte[] line = new byte[256];
int i = in.readLine(line, 0, 256);
//System.out.println("line: " + line);
//System.out.println("line i : " + i);
if(i
传出错!提示:
文件上传失败,该文件不存在!
下面是:Bean的原码,帮一把!谢谢!!!
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.*;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.*;
public class FileUploadBean
{
private String savePath, filepath, filename, contentType, saveFileName;
private Dictionary fields;
public String getFilename()
{
return filename;
}
public String getSaveFileName()
{
return saveFileName;
}
public String getFilepath()
{
return filepath;
}
public void setSavePath(String savePath)
{
this.savePath = savePath;
}
public String getContentType()
{
return contentType;
}
public String getFieldValue(String fieldName)
{
if (fields == null || fieldName == null)
return null;
return (String) fields.get(fieldName);
}
private void setFilename(String s)
{
if (s==null)
return;
int pos = s.indexOf("filename="");
if (pos != -1)
{
filepath = s.substring(pos+10, s.length()-1);
//System.out.println("filepath: " + filepath);
File f = new File(filepath);
if (f.exists())
{
//System.out.println("filepath exist: " + filepath);
}
else
{
filepath = "";
//System.out.println("filepath not exist: " + filepath);
}
// Windowsä¯ÀÀÆ÷·¢ËÍÍêÕûµÄÎļþ·¾¶ºÍÃû×Ö,µ«Linux/UnixºÍMacä¯ÀÀÆ÷Ö»·¢ËÍÎļþÃû×Ö
pos = filepath.lastIndexOf("\");
if(pos != -1) filename = filepath.substring(pos + 1);
else filename = filepath;
//System.out.println("filename: " + filename);
}
}
private void setContentType(String s)
{
if (s==null)
return;
int pos = s.indexOf(": ");
if(pos != -1) contentType = s.substring(pos+2, s.length());
}
public void doUpload(HttpServletRequest request) throws IOException
{
ServletInputStream in = request.getInputStream();
byte[] line = new byte[256];
int i = in.readLine(line, 0, 256);
//System.out.println("line: " + line);
//System.out.println("line i : " + i);
if(i