package com.zxz.pgdemo.controller;
import com.zxz.pgdemo.entity.User;
import com.zxz.pgdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import static reactor.core.publisher.Signal.complete;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
protected UserMapper userMapper;
@RequestMapping(value = "/findByName", method = RequestMethod.POST)
List<User> findByName(@RequestBody User user) {
return userMapper.findByName(user.getUserName());
}
void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletInputStream inputStream = request.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
AsyncContext asyncContext = request.startAsync();
InputStream is = new FileInputStream("");
outputStream.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
byte[] buffer = new byte[4096];
while(outputStream.isReady())
{
int len=is.read(buffer);
if (len<0)
{
asyncContext.complete();
return;
}
outputStream.write(buffer,0,len);
}
}
@Override
public void onError(Throwable throwable) {
asyncContext.complete();
try {
is.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String info = request.getPathInfo();
response.setContentType(request.getServletContext().getMimeType(info));
File file = new File(request.getPathTranslated());
response.setContentLengthLong(file.length());
// Look for a file mapped buffer in the cache
ByteBuffer mapped = cache.get(path);
if (mapped == null) {
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
ByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
mapped = cache.putIfAbsent(path, buf);
if (mapped == null)
mapped = buf;
}
}
// write the buffer asynchronously
final ByteBuffer content = mapped.asReadOnlyBuffer();
final ServletOutputStream out = response.getOutputStream();
final AsyncContext async = request.startAsync();
out.setWriteListener(new WriteListener() {
public void onWritePossible() throws IOException {
while (out.isReady()) {
if (!content.hasRemaining()) {
async.complete();
return;
}
out.write(content);
}
}
@Override
public void onError(Throwable t) {
request.getServletContext().log("Async Error", t);
async.complete();
}
});
}
}
tomcat-nio
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.Tomcat总体架构 Tomcat有Connector和Container两大核心组件,Connector组件...