springboot怎么把数据传给前端:springboot 如何引入前端 bootstrap?
这种问题网上一搜一大堆,你可以具体找一篇文章试试,遇到问题可以针对相关问题去提问。springboot通过jar包方式引入bootstrap_个人文章 - SegmentFault 思否 这不是查查就
顺晟科技
2022-10-19 13:39:26
194
编辑:想通了。我使用PrinterWriter来编写标题,并使用OutputStream来编写内容。我使用OutputStream来编写头、内容和加载的图像。
我正在使用ServerSocket用Java创建一个简单的服务器。当我运行代码并连接到localhost站点时,它会正确地显示我的html和css,但不显示我的本地宿主图像,并显示alt文本。
当我在没有服务器的情况下通过firefox打开html文件时,图像会加载。我99%肯定文件路径是正确的,因为其他文件(即我的css文件)检索是正确的。
当直接链接到熊(警告:可爱的熊)时,熊会在我的html中显示。
有什么想法吗?谢谢。
以下是我的文件布局: 文件布局
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
// HttpServer object contains a ServerSocket object, representing the server, and an
// ArrayList which contains the sockets connected
private ServerSocket server_;
private ArrayList<Socket> sockets_ = new ArrayList<>();
// Starts the server and runs a while loop waiting for sockets to connect. If a socket
// connects, serve the client the requested page or index.html
public static void main(String[] args) throws IOException {
HttpServer testServer = new HttpServer(8080);
boolean cont = true;
while(cont){
Request newRequest = testServer.AcceptSocket();
Response newResponse = testServer.generateResponse(newRequest);
cont = testServer.Respond(newResponse);
}
}
// HttpServer constructor that makes a server at the specified port
public HttpServer(int port) throws IOException {
server_ = new ServerSocket(port);
}
// Waits for a client socket to connect. Returns a Request object representing the
// client's request
public Request AcceptSocket() throws IOException {
sockets_.add(server_.accept());
return new Request(sockets_.get(sockets_.size()-1).getInputStream());
}
// Returns a Response object when given a Request object
public Response generateResponse(Request newRequest) throws IOException {
return new Response(new File("Resources" + newRequest.getPath_()));
}
// Writes out Response to the most recently connected client socket based on the given
// Response object
public boolean Respond(Response newResponse) throws IOException {
try{
OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(newResponse.getHeader_());
writer.print(newResponse.getResponse_());
writer.flush();
writer.close();
sockets_.get(sockets_.size()-1).close();
return true;
}
catch(FileNotFoundException e){
return false;
}
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
// HttpServer object contains a ServerSocket object, representing the server, and an
// ArrayList which contains the sockets connected
private ServerSocket server_;
private ArrayList<Socket> sockets_ = new ArrayList<>();
// Starts the server and runs a while loop waiting for sockets to connect. If a socket
// connects, serve the client the requested page or index.html
public static void main(String[] args) throws IOException {
HttpServer testServer = new HttpServer(8080);
boolean cont = true;
while(cont){
Request newRequest = testServer.AcceptSocket();
Response newResponse = testServer.generateResponse(newRequest);
cont = testServer.Respond(newResponse);
}
}
// HttpServer constructor that makes a server at the specified port
public HttpServer(int port) throws IOException {
server_ = new ServerSocket(port);
}
// Waits for a client socket to connect. Returns a Request object representing the
// client's request
public Request AcceptSocket() throws IOException {
sockets_.add(server_.accept());
return new Request(sockets_.get(sockets_.size()-1).getInputStream());
}
// Returns a Response object when given a Request object
public Response generateResponse(Request newRequest) throws IOException {
return new Response(new File("Resources" + newRequest.getPath_()));
}
// Writes out Response to the most recently connected client socket based on the given
// Response object
public boolean Respond(Response newResponse) throws IOException {
try{
OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(newResponse.getHeader_());
writer.print(newResponse.getResponse_());
writer.flush();
writer.close();
sockets_.get(sockets_.size()-1).close();
return true;
}
catch(FileNotFoundException e){
return false;
}
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
// HttpServer object contains a ServerSocket object, representing the server, and an
// ArrayList which contains the sockets connected
private ServerSocket server_;
private ArrayList<Socket> sockets_ = new ArrayList<>();
// Starts the server and runs a while loop waiting for sockets to connect. If a socket
// connects, serve the client the requested page or index.html
public static void main(String[] args) throws IOException {
HttpServer testServer = new HttpServer(8080);
boolean cont = true;
while(cont){
Request newRequest = testServer.AcceptSocket();
Response newResponse = testServer.generateResponse(newRequest);
cont = testServer.Respond(newResponse);
}
}
// HttpServer constructor that makes a server at the specified port
public HttpServer(int port) throws IOException {
server_ = new ServerSocket(port);
}
// Waits for a client socket to connect. Returns a Request object representing the
// client's request
public Request AcceptSocket() throws IOException {
sockets_.add(server_.accept());
return new Request(sockets_.get(sockets_.size()-1).getInputStream());
}
// Returns a Response object when given a Request object
public Response generateResponse(Request newRequest) throws IOException {
return new Response(new File("Resources" + newRequest.getPath_()));
}
// Writes out Response to the most recently connected client socket based on the given
// Response object
public boolean Respond(Response newResponse) throws IOException {
try{
OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(newResponse.getHeader_());
writer.print(newResponse.getResponse_());
writer.flush();
writer.close();
sockets_.get(sockets_.size()-1).close();
return true;
}
catch(FileNotFoundException e){
return false;
}
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
// HttpServer object contains a ServerSocket object, representing the server, and an
// ArrayList which contains the sockets connected
private ServerSocket server_;
private ArrayList<Socket> sockets_ = new ArrayList<>();
// Starts the server and runs a while loop waiting for sockets to connect. If a socket
// connects, serve the client the requested page or index.html
public static void main(String[] args) throws IOException {
HttpServer testServer = new HttpServer(8080);
boolean cont = true;
while(cont){
Request newRequest = testServer.AcceptSocket();
Response newResponse = testServer.generateResponse(newRequest);
cont = testServer.Respond(newResponse);
}
}
// HttpServer constructor that makes a server at the specified port
public HttpServer(int port) throws IOException {
server_ = new ServerSocket(port);
}
// Waits for a client socket to connect. Returns a Request object representing the
// client's request
public Request AcceptSocket() throws IOException {
sockets_.add(server_.accept());
return new Request(sockets_.get(sockets_.size()-1).getInputStream());
}
// Returns a Response object when given a Request object
public Response generateResponse(Request newRequest) throws IOException {
return new Response(new File("Resources" + newRequest.getPath_()));
}
// Writes out Response to the most recently connected client socket based on the given
// Response object
public boolean Respond(Response newResponse) throws IOException {
try{
OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(newResponse.getHeader_());
writer.print(newResponse.getResponse_());
writer.flush();
writer.close();
sockets_.get(sockets_.size()-1).close();
return true;
}
catch(FileNotFoundException e){
return false;
}
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
// Hosts an httpserver located in the directory ../website. Clients can connect to receive
// Specific page. If the page requested doesn't exist, serve index.html
public class HttpServer {
// HttpServer object contains a ServerSocket object, representing the server, and an
// ArrayList which contains the sockets connected
private ServerSocket server_;
private ArrayList<Socket> sockets_ = new ArrayList<>();
// Starts the server and runs a while loop waiting for sockets to connect. If a socket
// connects, serve the client the requested page or index.html
public static void main(String[] args) throws IOException {
HttpServer testServer = new HttpServer(8080);
boolean cont = true;
while(cont){
Request newRequest = testServer.AcceptSocket();
Response newResponse = testServer.generateResponse(newRequest);
cont = testServer.Respond(newResponse);
}
}
// HttpServer constructor that makes a server at the specified port
public HttpServer(int port) throws IOException {
server_ = new ServerSocket(port);
}
// Waits for a client socket to connect. Returns a Request object representing the
// client's request
public Request AcceptSocket() throws IOException {
sockets_.add(server_.accept());
return new Request(sockets_.get(sockets_.size()-1).getInputStream());
}
// Returns a Response object when given a Request object
public Response generateResponse(Request newRequest) throws IOException {
return new Response(new File("Resources" + newRequest.getPath_()));
}
// Writes out Response to the most recently connected client socket based on the given
// Response object
public boolean Respond(Response newResponse) throws IOException {
try{
OutputStream output = sockets_.get(sockets_.size()-1).getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
writer.println(newResponse.getHeader_());
writer.print(newResponse.getResponse_());
writer.flush();
writer.close();
sockets_.get(sockets_.size()-1).close();
return true;
}
catch(FileNotFoundException e){
return false;
}
}
}
顺晟科技:
答案是使用OutputStream write()函数写出已读取的文件。对文件的头和内容都使用write()。
我的错误是将PrintWriter用作标题,而将write()用作内容,这是行不通的。
05
2022-12
02
2022-12
02
2022-12
29
2022-11
29
2022-11
24
2022-11