你只需要进行Http-FileUpload,这是POST请求的特殊情况。
不需要uuencode文件。
不需要使用特殊的lib/jar。
不需要将对象保存到磁盘(无论以下示例是否这样做)。
在
使用java.net.URLConnection发送和处理HTTP请求下找到关于Http-Command以及"file upload"的非常好的解释。
从那里可以找到文件上传示例(查看"send binary file"),也可以添加一些附属数据。
String param = "value";
File textFile = new File("/path/to/file.txt");
File binaryFile = new File("/path/to/file.bin");
String boundary = Long.toHexString(System.currentTimeMillis());
String CRLF = "\r\n";
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
OutputStream output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(param).append(CRLF).flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).flush();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset));
for (String line; (line = reader.readLine()) != null;) {
writer.append(line).append(CRLF);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
writer.flush();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
InputStream input = null;
try {
input = new FileInputStream(binaryFile);
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.flush();
} finally {
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
writer.append(CRLF).flush();
writer.append("--" + boundary + "--").append(CRLF);
} finally {
if (writer != null) writer.close();
}
关于您问题的第二部分。当成功上传文件(我使用apache common files)时,将blob作为图像传递并不是什么大问题。
以下是如何在servlet中接受文件。
public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator (pRequest);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fieldName = item.getFieldName();
InputStream stream = item.openStream();
....
stream.close();
}
...
这段代码会展示一张图片
public void doGet (HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException {
try {
Blob img = (Blob) entity.getProperty(propImg);
pResponse.addHeader ("Content-Disposition", "attachment; filename=abc.png");
pResponse.addHeader ("Cache-Control", "max-age=120");
String enc = "image/png";
pResponse.setContentType (enc);
pResponse.setContentLength (img.getBytes().length);
OutputStream out = pResponse.getOutputStream ();
out.write (img.getBytes());
out.close();
我希望这些代码片段能够帮助回答你的问题。