前端页面访问后台,后台直接读取文件,通过文件流将文件写入到页面
如:E://upload/index.html
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping(value="tempDownLoad")
public void tempDownLoad(HttpServletRequest request,HttpServletResponse response){
InputStream in = null;
try{
in = new FileInputStream("E://upload/index.html");
byte[] buffer = new byte[1024];
int bytesRead = 0;
do {
bytesRead = in.read(buffer, 0, buffer.length);
response.getOutputStream().write(buffer, 0, bytesRead);
} while (bytesRead == buffer.length);
} catch (Exception e) {
}finally{
try {
if (in != null) {
in.close();
}
response.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
logger.error("关闭文件IOException!");
}
}
}