1

有点难以置信,Java 让客户端变得如此简单。我确信我将不得不将图像发布到服务器文件以首先对其进行清理,但我也想阻止任何人访问这样的服务器文件(并且想了一点)并最终用图片向我发送垃圾邮件。我需要严格的流程,最终用户没有潜在的篡改。

如果我理解正确,从我读到的内容来看,PUT 应该正是我所需要的。我的签名 ja 从客户端向服务器发送与站点相关的信息图像,而不会暴露我的任何 FTP 信息或显示可能受到攻击的上传文件。我猜我是否正确掌握了 PUT 是我的问题。它会通过 http 将图像放在我的帐户上,而无需额外的处理过程?

顺便说一句,我怎么能把这个片段用于图像?

URLConnection urlconnection=null;
try{
File file = new File("C:/test.txt");
URL url = new URL("http://192.168.5.27/Test/test.txt");
urlconnection = url.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);

if (urlconnection instanceof HttpURLConnection) {
try {
((HttpURLConnection)urlconnection).setRequestMethod("PUT");
((HttpURLConnection)urlconnection).setRequestProperty("Content-type", "text/html");
((HttpURLConnection)urlconnection).connect();


} catch (ProtocolException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}
}


BufferedOutputStream bos = new BufferedOutputStream(urlconnection
 .getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
 file));
int i;
// read byte by byte until end of stream
while ((i = bis.read()) >0) {
 bos.write(i);
}
System.out.println(((HttpURLConnection)urlconnection).getResponseMessage());
}
catch(Exception e)
{
e.printStackTrace();
}
try {

InputStream inputStream;
int responseCode=((HttpURLConnection)urlconnection).getResponseCode();
if ((responseCode>= 200) &&(responseCode<=202) ) {
inputStream = ((HttpURLConnection)urlconnection).getInputStream();
int j;
while ((j = inputStream.read()) >0) {
 System.out.println(j);
}

} else {
inputStream = ((HttpURLConnection)urlconnection).getErrorStream();
}
((HttpURLConnection)urlconnection).disconnect();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4

1 回答 1

0

不,绝大多数 Web 服务器并没有像您想象的那样实现 PUT 命令。PUT 命令在 WebDAV 协议或自定义 REST API 之外通常没有用处。

于 2012-04-06T18:44:47.110 回答