///////以下是客户端向服务器端写文件//////////////////////////////////////////
DataOutputStream dstream=new DataOutputStream(myHttpCon.getOutputStream());
FileInputStream fis1=null;
File file1=new File(filePath);
fis1=new FileInputStream(file1);
byte[] filebuff=new byte[2048];
int howMany=-1;
int totMany=0;
howMany=fis1.read(filebuff,0,2048);
dstream.write("filebegin\r\n".getBytes());
while(howMany!=-1)
{
totMany+=howMany;
dstream.write(filebuff,0,howMany);
System.out.println(new String(filebuff));
howMany=fis1.read(filebuff,0,2048);
}
dstream.write("\r\n".getBytes());
dstream.write("fileend\r\n".getBytes());
System.err.println("read"+totMany+"bytes from file,wrote to outputstream.");
fis1.close();;
dstream.close();//////////////////////////////////////////////////////////////////////////////////////////以下是服务器端的接收文件的写法,有点长,判断了几个状态。/////////////////////// public httpRequestHandler(Socket ssocket) throws Exception
{
this.socket=ssocket;
this.input=socket.getInputStream();
this.output=new PrintStream(socket.getOutputStream());
this.br=new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
String prefix=System.getProperty("user.dir")+System.getProperty("file.separator")+"uploadfile"+System.getProperty("file.separator");;
String newfilename="";
String downfilename="";
boolean isfile=false;
String operateType="";
StringBuffer buf=new StringBuffer();
boolean isoverfile=false;
byte[] buffer = new byte[2048];
try
{
while( true)
{
if(isoverfile==true)
{
break;
}
input.read(buffer);
String msg = new String(buffer);
String[] temparr=msg.split("\r\n"); if(temparr!=null)
{
for(int i=0;i<temparr.length;i++)
{
if(temparr[i].indexOf("operatetype")!=-1)
{
operateType=getKeyValue(temparr[i],"operatetype");
}
if(operateType.equals("0")) //upload file begin
{
if (temparr[i].indexOf("filename") != -1)
{
newfilename = getKeyValue(temparr[i], "filename");
}
if (temparr[i].indexOf("fileend") != -1)
{
isoverfile = true;
break;
}
if (isfile == true)
{
System.out.println("file content="+temparr[i]);
buf.append(temparr[i] + "\r\n");
}
if (temparr[i].indexOf("filebegin") != -1) {
isfile = true;
}
} // upload file end;
}
}
}
if(operateType.equals("0"))
{
prefix+=newfilename;
String retv=SaveFile(prefix,buf.toString());
if(retv.equals("ok"))
{
output.println("file is accept ok!");
output.println("file is save ok!");
output.write(("savefilepath:"+prefix+"\r\n").getBytes());
output.write("end".getBytes());
}
else
{
output.println("file is accept ok!");
output.println("file is save error!");
output.write(("savefilepath:"+prefix+"\r\n").getBytes());
output.write("end".getBytes());
}
}
output.close();
input.close();
br.close(); }
catch(Exception ex)
{
ex.printStackTrace();
}
} public String getKeyValue(String line,String keystr)
{
int startindex=(keystr+":").length();
String retv="";
if(startindex!=-1)
{
retv=line.substring(startindex).trim();
}
return retv;
}
public String SaveFile(String filepath,String filecontent)
{
String retv="error";
try
{
FileWriter fw = new FileWriter(filepath, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(filecontent);
pw.flush();
pw.close();
fw.close();
retv="ok";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return retv;
}
///////////////////////////////////////////////////////////////
DataOutputStream dstream=new DataOutputStream(myHttpCon.getOutputStream());
FileInputStream fis1=null;
File file1=new File(filePath);
fis1=new FileInputStream(file1);
byte[] filebuff=new byte[2048];
int howMany=-1;
int totMany=0;
howMany=fis1.read(filebuff,0,2048);
dstream.write("filebegin\r\n".getBytes());
while(howMany!=-1)
{
totMany+=howMany;
dstream.write(filebuff,0,howMany);
System.out.println(new String(filebuff));
howMany=fis1.read(filebuff,0,2048);
}
dstream.write("\r\n".getBytes());
dstream.write("fileend\r\n".getBytes());
System.err.println("read"+totMany+"bytes from file,wrote to outputstream.");
fis1.close();;
dstream.close();//////////////////////////////////////////////////////////////////////////////////////////以下是服务器端的接收文件的写法,有点长,判断了几个状态。/////////////////////// public httpRequestHandler(Socket ssocket) throws Exception
{
this.socket=ssocket;
this.input=socket.getInputStream();
this.output=new PrintStream(socket.getOutputStream());
this.br=new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
String prefix=System.getProperty("user.dir")+System.getProperty("file.separator")+"uploadfile"+System.getProperty("file.separator");;
String newfilename="";
String downfilename="";
boolean isfile=false;
String operateType="";
StringBuffer buf=new StringBuffer();
boolean isoverfile=false;
byte[] buffer = new byte[2048];
try
{
while( true)
{
if(isoverfile==true)
{
break;
}
input.read(buffer);
String msg = new String(buffer);
String[] temparr=msg.split("\r\n"); if(temparr!=null)
{
for(int i=0;i<temparr.length;i++)
{
if(temparr[i].indexOf("operatetype")!=-1)
{
operateType=getKeyValue(temparr[i],"operatetype");
}
if(operateType.equals("0")) //upload file begin
{
if (temparr[i].indexOf("filename") != -1)
{
newfilename = getKeyValue(temparr[i], "filename");
}
if (temparr[i].indexOf("fileend") != -1)
{
isoverfile = true;
break;
}
if (isfile == true)
{
System.out.println("file content="+temparr[i]);
buf.append(temparr[i] + "\r\n");
}
if (temparr[i].indexOf("filebegin") != -1) {
isfile = true;
}
} // upload file end;
}
}
}
if(operateType.equals("0"))
{
prefix+=newfilename;
String retv=SaveFile(prefix,buf.toString());
if(retv.equals("ok"))
{
output.println("file is accept ok!");
output.println("file is save ok!");
output.write(("savefilepath:"+prefix+"\r\n").getBytes());
output.write("end".getBytes());
}
else
{
output.println("file is accept ok!");
output.println("file is save error!");
output.write(("savefilepath:"+prefix+"\r\n").getBytes());
output.write("end".getBytes());
}
}
output.close();
input.close();
br.close(); }
catch(Exception ex)
{
ex.printStackTrace();
}
} public String getKeyValue(String line,String keystr)
{
int startindex=(keystr+":").length();
String retv="";
if(startindex!=-1)
{
retv=line.substring(startindex).trim();
}
return retv;
}
public String SaveFile(String filepath,String filecontent)
{
String retv="error";
try
{
FileWriter fw = new FileWriter(filepath, true);
PrintWriter pw = new PrintWriter(fw);
pw.println(filecontent);
pw.flush();
pw.close();
fw.close();
retv="ok";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return retv;
}
///////////////////////////////////////////////////////////////
解决方案 »
- 现在写项目 用到 hibernatetemplate
- struts-dojo-plugins问题
- spring3.0+AOP记录日志,没有进入切面类里的方法,请各位高抬贵手指点指点
- 关于Hibernate两个表非主键关联后的数据更新问题?
- 初学struts2感觉标签太难用了
- 怎样跨项目访问资源文件?
- js的问题
- 可以给自己加分的吗?
- structs怪问题,来看看吧!
- No Hibernate Session bound to thread, and configuration does not allow creation
- 关于ANT里几个不问题看不懂,求指教
- 求一正则表达式 匹配<!--123-->中文<!--123-->
String msg = new String(buffer);你这里,不就把接收的数据buffer变成String了吗? 很多STRING型不能处理的二进制数据都丢了.
实际上,是从第一个碰到的00开始,以后的都被截掉了.
这些代码是你粘来的吧,粘来的也要自己能读懂,才能根据自己的要求进行修改啊.
多看看JAVA的文件操作吧.这里不是学校,没法说太多.
最好在http协议中先传入长度,然后按长度接收。