Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from 老馬/WebJava
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
WebJava
/
Web
/
Response.java
WebJava
/
Web
/
Response.java
Response.java 5.43 KB
Copy Edit Raw Blame History
Ma authored 2015年06月12日 09:41 +08:00 . 添加Demo
package Web;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
/**
* 用于记录一些相应信息 如状态码,cookie等
*
* @author Ma
*/
public class Response {
/**
* 返回消息实体
*/
public static String Content = "";
/**
* 如果是文件就直接些进去
*/
public static byte[] FileBytes;
/**
* 标记这次是不是请求文件的
*/
public static boolean IsFile = false;
public static ArrayList<String> Header = new ArrayList<String>();
public static String Status = "HTTP/1.1 200 OK";
public static ArrayList<String> Cookies = new ArrayList<String>();
public static String ContentType = "text/html;charset=UTF-8;";
/**
* 服务器的名字
*/
public static String Server = Options.ServerName;
/**
* 只包含Contest的长度不包含header的长度
*/
public static int ContentLength;
/**
* //禁用页面缓存 <br/>
* header('Cache-Control: no-cache, no-store, max-age=0,must-revalidate');
* <br/>
* header('Expires: 1997年7月26日 05:00:00 GMT');<br/>
* header('Pragma: no-cache');<br/>
*
* public static String X-Powered-By;
*
*/
/**
* 设置状态码 HTTP/1.1 200 OK <br/>
* HTTP/1.1 303 See Other \r\nLocation:http://www.maiyoule.com/ 结尾没有分号 <br/>
* HTTP/1.1 304 Not Modified 缓存 <br/>
* HTTP/1.1 403 Forbidden<br/>
* HTTP/1.1 404 Not Found <br/>
* HTTP/1.1 500 Internal Server Error<br/>
*
* @param code
*/
public static void setStatus(int code) {
String string;
switch (code) {
case 200:
string = "HTTP/1.1 200 OK";
break;
case 303:
string = "HTTP/1.1 303 See Other";
break;
case 304:
string = "HTTP/1.1 304 Not Modified";
break;
case 403:
string = "HTTP/1.1 403 Forbidden";
break;
case 404:
string = "HTTP/1.1 404 Not Found";
break;
case 500:
string = "HTTP/1.1 500 Internal Server Error";
break;
default:
string = "HTTP/1.1 200 OK";
}
Response.Status = string;
}
/**
* 设置content-length长度
*
* @param length
*/
public static void setContentLength(int length) {
Response.ContentLength = length;
}
/**
* 设置ContentType application/json; charset=utf-8; <br/>
* text/html;charset=UTF-8;<br/>
* application/xml;charset=UTF-8;<br/>
*/
public static void setContentType(String type) {
Response.ContentType = type;
}
/**
* 查找ContentType类型 不能识别的文件,默认当作zip处理
*
* @todo 是否允许自己配置
*/
public static String findContentType(String type) {
HashMap<String, String> contentType = new HashMap<String, String>();
contentType.put("html", "text/html;charset=UTF-8;");
contentType.put("json", "application/json; charset=utf-8;");
contentType.put("xml", "application/xml;charset=UTF-8;");
contentType.put("zip", "application/x-zip-compressed");
contentType.put("ico", "image/x-icon");
String returnType = "";
if (contentType.containsKey(type)) {
returnType = contentType.get(type);
} else {
returnType = "application/x-zip-compressed";
}
return returnType;
}
/**
* 设置返回实体
*
* @param content
*/
public static void setContent(String content) {
Response.Content = content;
}
/**
* 追加返回实体
*
* @param content
*/
public static void addContent(String content) {
Response.Content = Response.Content + content;
}
/**
* 添加自定义Header
*
* @param header
* @return
*/
public static boolean header(String header) {
return Response.Header.add(header);
}
/**
* 获取此次响应的header
*
* @return
*/
public static String getHeader() {
String header;
header = Response.Status + "\r\n";
//检查cookie
for (int i = 0; i < Response.Cookies.size(); i++) {
header += Response.Cookies.get(i) + "\r\n";
}
header += "Content-type: " + Response.ContentType + "\r\n";
header += "Content-ength: " + Response.ContentLength + "\r\n";
//检查自定义Header
for (int i = 0; i < Response.Header.size(); i++) {
header += Response.Header.get(i) + "\r\n";
}
header += "Server: " + Response.Server + "\r\n\r\n";
return header;
}
public static byte[] getContentBytes() throws UnsupportedEncodingException {
return Response.Content.getBytes("UTF-8");
}
/**
* 请求结束,清理信息
*/
public static void finish() {
Response.Status = "HTTP/1.1 200 OK";
Response.Content = "";
Response.Header = new ArrayList<String>();
Response.Cookies = new ArrayList<String>();
Response.ContentLength = 0;
Response.ContentType = "text/html;charset=UTF-8;";
Response.FileBytes = new byte[0];
//取消是文件标识
Response.IsFile = false;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Web.Java是一个高性能的Java服务器。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tonytwain/WebJava.git
git@gitee.com:tonytwain/WebJava.git
tonytwain
WebJava
WebJava
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /