实现文件的上传的第一种方式web
(经过servlet实现文件的上传)浏览器
1:文件的上传(以上传图片为例)app
Jsp代码:dom
<form action="UploadServlet" method="post"enctype="multipart/jsp
form-data">post
上传文件:<input type="file" name="file1"><br>url
上传文件:<input type="file" name="file2"><br>spa
上传文件:<input type="file" name="file3"><br>pwa
${result};//显示上传结果指针
<input type="submit" value="提交">
</form><hr>
Servlet代码:
public class UploadServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("已接收到请求");
//从request当中获取流信息
InputStream fileSource = req.getInputStream();
String tempFileName = "E:/tempFile”;
//tempFile指向临时文件
File tempFile = new File(tempFileName);
//outputStram文件输出流指向这个临时文件
FileOutputStream outStream = new FileOutputStream(tempFile);
byte b[] = new byte[1024];
int n;
while(( n = fileSource.read(b)) != -1){
outStream.write(b, 0, n);
}
//关闭输出流、输入流
outputStream.close();
fileSource.close();
//获取上传文件的名称
RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
randomFile.readLine();//读取临时文件的第一行
String str = randomFile.readLine();//读取临时文件的第二行
//不一样的浏览器下读取文件的名称的方法不同,能够用httpwatch肯定
int begin = str.lastIndexOf("\\") + 1;
int end = str.lastIndexOf("\"");
String filename = str.substring(begin, end);
System.out.println("filename:" + filename);
//从新定位文件指针到文件头
randomFile.seek(0);
long startPosition = 0;
int i = 1;
//获取文件内容 开始位置
while(( n = randomFile.readByte()) != -1 && i <=4){
if(n == '\n'){
startPosition = randomFile.getFilePointer();
i ++;
}
}
startPosition = randomFile.getFilePointer() -1;
//获取文件内容 结束位置
randomFile.seek(randomFile.length());
long endPosition = randomFile.getFilePointer();
int j = 1;
while(endPosition >=0 && j<=2){
endPosition--;
randomFile.seek(endPosition);
if(randomFile.readByte() == '\n'){
j++;
}
}
endPosition = endPosition -1;
//设置保存上传文件的路径
String realPath = getServletContext().getRealPath("/") + "images";
File fileupload = new File(realPath);
if(!fileupload.exists()){
fileupload.mkdir();
}
File saveFile = new File(realPath,filename);
RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
//从临时文件当中读取文件内容(根据起止位置获取)
randomFile.seek(startPosition);
while(startPosition < endPosition){
randomAccessFile.write(randomFile.readByte());
startPosition = randomFile.getFilePointer();
}
//关闭输入输出流、删除临时文件
randomAccessFile.close();
randomFile.close();
tempFile.delete();
req.setAttribute("result", "上传成功!");
RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
dispatcher.forward(req, resp);
}
}
2文件的下载
1:jsp代码
下载:(txt.txt已放到images目录下)
<a href="download.do?filename=text.txt">text.txt</a>${erro}
Servlet代码:
public class Download extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path=getServletContext().getRealPath("/")+"images/";
String filename=req.getParameter("filename");
File file=new File(path+filename);
if(file.exists()){
//设置相应类型application/octet-stream
resp.setContentType("application/x-msdownload");
//设置头信息
resp.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
InputStream inputStream = new FileInputStream(file);
ServletOutputStream ouputStream = resp.getOutputStream();
byte b[] = new byte[1024];
int n ;
while((n = inputStream.read(b)) != -1){
ouputStream.write(b,0,n);
}
//关闭流、释放资源
ouputStream.close();
inputStream.close();
}
else{
req.setAttribute("erro", "文件不存在,下载失败!");
RequestDispatcher dis=req.getRequestDispatcher("index.jsp");
dis.forward(req, resp);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
《web.xml文件生成,注意action与url-pattern 一致》