java客户端通过http发送POST请求上传文件 20
java客户端代码:importjavax.swing.*;importjava.util.*;importjava.io.*;importjava.net.*;impo...
java客户端代码:
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
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代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class TestServlet3 extends HttpServlet
{
private static final long serialVersionUID=1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void destroy(){
super.destroy();
}
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客户端上传不了文件。我试了下载可以,上传不可以。 展开
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
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代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class TestServlet3 extends HttpServlet
{
private static final long serialVersionUID=1L;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void destroy(){
super.destroy();
}
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
展开全部
这样的写法,是直接socket的做法。
如果是HTTP的,要按HTTP的协议进行。先了解一下multi-part的post
如果是HTTP的,要按HTTP的协议进行。先了解一下multi-part的post
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询