Struts2(11):Struts2的上传下载

首先来看看在JSP中上传是如何做的:

从upload.jsp中选择文件上传,在result.jsp中读取上传文件的内容,注意:在form中,method必须使用post,enctype必须使用multipart/form-data

<%@ page language="java" pageEncoding="gb2312"%>
<html>
  <body>
	<form action="result.jsp" method="post" enctype="multipart/form-data">
		File:<input type="file" name="file"><br>
		<input type="submit" name="submit" value="submit">
	</form>    
  </body>
</html>

 

result.jsp

<%@ page language="java"  pageEncoding="gb2312"%>
<%@ page import="java.io.*"%>
<html>
  <body>
<%
	InputStream is = request.getInputStream();
	BufferedReader bf = new BufferedReader(new InputStreamReader(is));
	String buff = null;
	while((buff = bf.readLine()) != null)
	{
		out.print(buff+"<br>");
	}
 %>
  </body>
</html>

 

Struts2的上传下载依赖Apache common开源项目。具体是以下两个包:

commons-fileupload-1.2.2-bin.zip

下载地址:http://commons.apache.org/io/download_io.cgi

commons-io-1.4-bin.zip

下载地址:http://commons.apache.org/fileupload/download_fileupload.cgi

 

 下面使用fileupload包,编写一个servlet,接收上传的文件,并放入临时文件夹中存放

package com.test.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//取得WebToot目录下的upload目录
		String path = req.getRealPath("/upload");
		//设置临时文件存放目录
		factory.setRepository(new File(path));
		//设置阀值大小,低于此值的文件将直接读入内存,高于此值的将直接写入磁盘
		factory.setSizeThreshold(1024*1024);
		ServletFileUpload upload = new ServletFileUpload(factory);
		try{
			List<FileItem> list = upload.parseRequest(req);
			//取出所有的表单,如从upload.jsp页面转来的username,password,file1,file2
			for(FileItem item:list){
				//判断是否是非file类型
				if(item.isFormField()){
					String name = item.getFieldName();
					//取出表单中的值
					String value = item.getString("gbk");
					req.setAttribute(name, value);
				}else{
					//取出路径及文件名:  C:\Users\Bruce\Desktop\Jobs.txt,有的浏览器只有文件名,没有路径名
					String name = item.getName();
					int start = name.lastIndexOf("\\");
					String fileName = name.substring(start+1);
					req.setAttribute(name, fileName);
					item.write(new File(path,fileName));
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		req.getRequestDispatcher("upload/result2.jsp").forward(req,resp);
	}
}

 在web.xml文件中进行配置

<servlet>
		<servlet-name>UploadServlet</servlet-name>
		<servlet-class>com.test.servlet.UploadServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>UploadServlet</servlet-name>
		<url-pattern>/UploadServlet</url-pattern>
	</servlet-mapping>

 

你可能感兴趣的:(apache,html,jsp,servlet,cgi)