我有獨立的Java客戶端使用httpURLconnection類將http數據發送到http servlet。但數據以非可打印字符格式顯示。將HTTP數據從HTTP Java客戶端發送到具有HttpURLConnection的servlet時出現問題
對於模擬我一直試圖發送簡單的字符串,但它仍然出現在非打印格式。
我寫了下面的客戶端代碼的servlet進行溝通,
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
public class HttpClient implements IClient {
private static IClient client = null;
private HttpURLConnection httpConn = null;
private OutputStream output = null;
private InputStream input = null;
private OutputStreamWriter out = null;
private HttpClient() {
}
public static IClient getHttpClient() {
if (client == null) {
client = new HttpClient();
}
return client;
}
@Override
public void connect(String urlString) throws IOException {
URL url = new URL(urlString);
httpConn = (HttpURLConnection) url.openConnection();
initConnection();
httpConn.connect();
output = httpConn.getOutputStream();
input = httpConn.getInputStream();
System.out.println("Connection Response:" + httpConn.getResponseCode());
}
@Override
public void sendFile(File file) throws IOException {
// BufferedOutputStream bos = new BufferedOutputStream(output);
// InputStream fis = new FileInputStream(file);
// int bytesRead = 0;
// byte[] buffer = new byte[8192];
// while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
// bos.write(buffer, 0, bytesRead);
// System.out.println("write:"+buffer);
// }
// bos.close();
// fis.close();
OutputStreamWriter out = new OutputStreamWriter(output, "UTF-16");
out.write("test me");
}
@Override
public boolean isConnected() {
return httpConn != null ? true : false;
}
@Override
public void close() {
httpConn.disconnect();
}
private void initConnection() throws ProtocolException {
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setUseCaches(false);
httpConn.setRequestProperty("Content-Type",
"text/xml; charset=\"UTF-16\"");
httpConn.setRequestProperty("Connection", "Keep-Alive");
}
private static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
System.out.println("\nDEBUG: FileInputStream is " + file);
// Get the size of the file
long length = file.length();
System.out.println("DEBUG: Length of " + file + " is " + length + "\n");
/*
* You cannot create an array using a long type. It needs to be an int
* type. Before converting to an int type, check to ensure that file is
* not loarger than Integer.MAX_VALUE;
*/
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large to process");
return null;
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while ((offset < bytes.length)
&& ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
return bytes;
}
}
和servlet代碼如下,
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class XMLServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("=========inside doPost=========");
// BufferedInputStream bis = new BufferedInputStream(req.getInputStream());
// OutputStream fos = new FileOutputStream("test.xml");
// int bytesRead = 0;
// byte[] buffer = new byte[8192];
// while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
// System.out.println("read:"+buffer);
// fos.write(buffer, 0, bytesRead);
// }
// fos.close();
// bis.close();
//
req.setCharacterEncoding("UTF-16");
InputStreamReader isr = new InputStreamReader(req.getInputStream(),"UTF-16");
char[] data = new char[10];
isr.read(data);
System.out.println(data);
for (char c : data) {
System.out.println(c);
}
}
}
請任何幫助我走出這個問題。
我喜歡`return httpConn!= null? true:false;`:) – khachik 2010-11-30 16:09:42