step1:写一个servlet
位置:就放在controller就行,其他地方应该也问题不大
注意
与一般的javaweb 不同,注释要这么写
@WebServlet(name = "list", urlPatterns = "/list")
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "list", urlPatterns = "/list")
public class DownloadListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("这是一个servlet");
}
}
step2:编辑启动器demoapplication
给sprintboot启动器加一个注释
@ServletComponentScan
作用是扫描同级和子目录下所有带
@WebServlet(name = "list", urlPatterns = "/list")
这种注释的
一开始这个注释会报错,红字
按 enter+alt导包解决报错
package com.documentstation.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
具体例子(文件传输站)和细节更改(直接把绝对路径写上)
String realPath ="C:\\Users\\17695\\IdeaProjects\\documentstation\\temp1";