Servlet3 文件上传demo
Servlet3 文件上传demo
夜来风雨急 发表于3个月前
Servlet3 文件上传demo
  • 发表于 3个月前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Upload File</title>
</head>
<body>

<form action="fupload" method="POST" enctype="multipart/form-data">
    <table align="center" border="1" width="50%" >
        <tr>
            <td>选择要上传的文件:</td>
            <td><input type="file" name="file"></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
            	<input type="submit" value="上传"> &nbsp;&nbsp;
            	<input type="button" value="查看已上传文件列表" onclick="window.location='flist'">            	
            </td>
        </tr>        
    </table>
</form>

</body>
</html>
package zbh;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.catalina.core.ApplicationPart;

@SuppressWarnings("serial")
@MultipartConfig
@WebServlet(name="UploadFile", urlPatterns="/fupload")
public class UploadServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();
		String path = this.getServletContext().getRealPath("/")+File.separator+"upload"+File.separator;
		Part p = request.getPart("file");
		ApplicationPart ap = (ApplicationPart) p;
		String fileName = ap.getSubmittedFileName();
		int path_idx = fileName.lastIndexOf("\\") + 1;
		
		fileName = fileName.substring(path_idx, fileName.length());
		
		String suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
		fileName = fileName.substring(0,fileName.lastIndexOf("."));
		System.out.println(fileName+"."+suffix);		
		p.write(path + fileName+"."+suffix);
		out.write("文件上传成功");		

	}
}




package zbh;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet(name="UploadList", urlPatterns="/flist")
public class UploadList extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();
		String path = this.getServletContext().getRealPath("/")+File.separator+"upload"+File.separator;
		
		out.write("<html><head>");
		out.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
		out.write("<title>已上传文件列表</title></head><body>");
		
		File dir = new File(path);
		for(File file:dir.listFiles()){
			out.write(file.getName()+"<br/>");
		}
		
		out.write("</body></html>");	
	}
}

 

共有 人打赏支持
粉丝 0
博文 51
码字总数 12749
×
夜来风雨急
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: