- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我编写了这个 HttpRequest 方法,但由于某种原因,它总是出现 404 Not Found,即使在 java 进程未运行时文件位置存在。
import java.io.*;
import java.net.*;
import java.util.*;
final class HttpRequest implements Runnable {
final static String CRLF = "\r\n";
Socket socket;
// Constructor
public HttpRequest(Socket socket) throws Exception {
this.socket = socket;
}
// Implement the run() method of the Runnable interface.
public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
private static void sendBytes(FileInputStream fis, OutputStream os)
throws Exception {
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024];
int bytes = 0;
// Copy requested file into the socket's output stream.
while((bytes = fis.read(buffer)) != -1 ) {
os.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName) {
if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
return "text/html";
}
if(fileName.endsWith(".jpeg") || fileName.endsWith(".jpg")) {
return "image/jpeg";
}
if(fileName.endsWith(".gif")) {
return "image/gif";
}
return "application/octet-stream";
}
private void processRequest() throws Exception {
// Get a reference to the socket's input and output streams.
InputStream is = socket.getInputStream();
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
// Set up input stream filters.
BufferedReader br = new BufferedReader(new InputStreamReader(is));
// Get the request line of the HTTP request message.
String requestLine = new String(br.readLine());
// Display the request line.
System.out.println();
System.out.println(requestLine);
// Get and display the header lines.
String headerLine = null;
while ((headerLine = br.readLine()).length() != 0) {
System.out.println(headerLine);
}
// Extract the filename from the request line.
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken(); // skip over the method, which should be "GET"
String fileName = tokens.nextToken();
// Prepend a "." so that file request is within the current directory.
fileName = "." + fileName;
// Open the requested file.
FileInputStream fis = null;
boolean fileExists = true;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
fileExists = false;
}
// Construct the response message.
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists) {
statusLine = "200 OK" + CRLF;
contentTypeLine = "Content-type: " +
contentType( fileName ) + CRLF;
} else {
statusLine = "404 NOT FOUND" + CRLF;
contentTypeLine = "Content Not Found!" + CRLF;
entityBody = "<HTML>" +
"<HEAD><TITLE>Not Found</TITLE></HEAD>" +
"<BODY>Not Found</BODY></HTML>";
}
// Send the status line.
os.writeBytes(statusLine);
// Send the content type line.
os.writeBytes(contentTypeLine);
// Send a blank line to indicate the end of the header lines.
os.writeBytes(CRLF);
// Send the entity body.
if (fileExists) {
sendBytes(fis, os);
fis.close();
} else {
os.writeBytes("File DNE: Content Not Found!");
}
// Close streams and socket.
os.close();
br.close();
socket.close();
}
}
任何帮助将不胜感激...我觉得这是我缺少的一些简单的东西。
最佳答案
我运行了你的代码;它工作得很好(除了一些没有正确打印标题的小问题,我尝试过的每个浏览器都愿意完全忽略)。
您确定您的工作目录位于您期望的位置吗?尝试将 404 消息更改为:
contentTypeLine = "Content Not Found: " + new File(fileName).getAbsolutePath() + CRLF;
作为引用,我使用以下测试工具运行它:
public static void main(String[] args) throws Exception {
final ServerSocket ss = new ServerSocket(8080);
while (true)
new HttpRequest(ss.accept()).run();
}
关于java - Java中的HttpRequest : Always goes to 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354815/
我发现很难决定某个东西是否应该成为域或应用程序的一部分。 通读 answer授权等概念有很大帮助,但我仍然发现自己在其他事情上苦苦挣扎。 为了说明我的困惑,请考虑一个发表评论的案例。以下是发表评论之前
虽然我是一名希望探索社交软件模式的程序员,但我也对它们的心理和社会学起源感兴趣。 我建议的格式是将每个回复限制为一次阅读,并简要说明为什么它很重要。 这个想法是,我们将能够看到哪些特定的读数最重要。由
我正在尝试编写一个数独生成器算法,这是我的 C++ 代码: void generateSudoku(num sudoku[][N]) { int i,j,k; int vett[N],n,
来 self 的书: .bss: Uninitialized global C variables 常见: Uninitalized data objects that are not yet all
在脚本结束之前打印内容时,从 php 脚本打印结果的方法是什么?我尝试使用输出缓冲区,将 sleep() 放在 echo 之间进行测试,但结果始终在脚本执行后显示。这也是浏览器端的事情吗? 最佳答案
我从 SonataAdminBundle 文档中的几个示例中重新组合了以下信息。如果有一些错误请纠正我,但这是我在 BlogBundle 的情况下得到的: 如您所见,一般来说,每个包都包含fron
早上好, 我正在编写一个 python 守护程序,需要将视频转换为 .mp4 格式。为此,我计划通过 Subprocess 使用 Handbrake,但我得到的结果好坏参半:有时它有效,有时该过程甚至
我有一个旨在进行反向传播的神经网络。 我一直在使用以下方法对隐藏层进行权重更新 Δwji = η × δj × yi 其中 δj 是 φ'(vj) * ej,其中 vj = Σi wjiyi,对于输出
我正在尝试将此 C++ 函数转换为 mips。我想我在循环中遇到了问题,因为当我运行它时,它给了我 13..1.17.5.. 但我的输出应该是两个 ip 地址:130.52.0.10 和 171.9.
我正在使用 position: fixed 和 bottom: 0 将一些东西附加到屏幕底部。然而,在我的 11"Air 上,这个名字似乎从屏幕上消失了,你可以看到网站 here。我已经在下面发布了我
我编写了这个 HttpRequest 方法,但由于某种原因,它总是出现 404 Not Found,即使在 java 进程未运行时文件位置存在。 import java.io.*; import ja
我有一个 img 元素用作导航栏的背景,然后我将在 img 元素的顶部显示导航链接。正如您将在下图中看到的,我的问题是保存链接的 ul 显示在导航栏的右侧,而不是显示在导航栏上方。为什么要这样做以及如
我正在试验 extern 和 extern "C" 一段时间,不小心在其中一个标识符中输入了错误 - $ 偷偷溜进来了。当我编译代码并得到一个 undefined symbol 的错误并最终看到导致它
我一直在搜索这个,但我似乎在这段代码中遇到了相同的文章: try { //some code } catch(Exception $e){
首先,它不是任何其他问题的重复,我已经阅读了有关该问题的 Angular 指南。但是我还有几个问题。 功能模块是最简单的模块——你有一个功能——将它分组到功能模块中。假设除了明显的功能外,我还拥有每个
管道中首先发生的事情是:URL 重写(如 IIS 的 URL 重写模块中所定义),还是 HttpHandler(通过 IIS7 的添加托管处理程序功能添加)? 最佳答案 重写模块首先执行。它会在选择任
目前,我使用 Azure 服务总线作为平台中不同服务之间的通信和保持数据一致性的手段。但是,假设我的一项服务(订阅者)长时间停机并且无法接收任何事件。突然间,该服务处于不一致的状态。 Azure 服务
每 N 次点击,出站链接就会发生变化的 PHP 或 JQuery 方式。 例如: Visitor1 单击 LinkA - 他们被带到 http://google.com 访客 2 单击 LinkA -
这应该很简单,但我误解了 CSS 行为。 searchbar-title-group 应该占据一整行,包含一个左对齐的标题和 2 个右对齐的按钮。按钮组是右对齐的,但它出现在下一行。为什么?我应该如何
一切正常,但我希望屏幕较小时居中对齐,它居中对齐,但屏幕之外还有一个空间出现。 代码: .example { line-height: 30px; background-color: #363
我是一名优秀的程序员,十分优秀!