Java客户端通过Http发送POST请求上传文件 10
java客户端:publicclassUpload{publicUpload(){StringfilePath="D:\\DataSocketServer.java";S...
java客户端:
public class Upload
{
public Upload(){
String filePath="D:\\DataSocketServer.java";
String fileName="DataSocketServer.java";
try{
URL url=new URL("http://localhost:8080//TestServlet3");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream());
File file=new File(filePath);
FileInputStream fis=new FileInputStream(file);
byte[] bytes=new byte[1024];
int numReadByte=0;
while((numReadByte=fis.read(bytes,0,1024))>0){
out.write(bytes,0,numReadByte);
}
out.flush();
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Upload();
}
}
servlet:
public class TestServlet3 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
BufferedInputStream in=new BufferedInputStream(request.getInputStream());
FileOutputStream fos=new FileOutputStream(new File("J:\\aaa.java"));
byte[] inputByte = new byte[1024];
int length=0;
while((length=in.read(inputByte,0,inputByte.length))>0){
fos.write(inputByte,0,length);
fos.flush();
}
fos.close();
}
}
为什么java客户端上传不了文件,我试了下载可以 展开
public class Upload
{
public Upload(){
String filePath="D:\\DataSocketServer.java";
String fileName="DataSocketServer.java";
try{
URL url=new URL("http://localhost:8080//TestServlet3");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream());
File file=new File(filePath);
FileInputStream fis=new FileInputStream(file);
byte[] bytes=new byte[1024];
int numReadByte=0;
while((numReadByte=fis.read(bytes,0,1024))>0){
out.write(bytes,0,numReadByte);
}
out.flush();
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new Upload();
}
}
servlet:
public class TestServlet3 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
BufferedInputStream in=new BufferedInputStream(request.getInputStream());
FileOutputStream fos=new FileOutputStream(new File("J:\\aaa.java"));
byte[] inputByte = new byte[1024];
int length=0;
while((length=in.read(inputByte,0,inputByte.length))>0){
fos.write(inputByte,0,length);
fos.flush();
}
fos.close();
}
}
为什么java客户端上传不了文件,我试了下载可以 展开
1个回答
2013-11-14
展开全部
要按http的multi-part上传的。接收端,再按multi-part解析成文件流
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询