REST中上传文件简单的实现笔记

package boa.framework.updown;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 * 
 * REST,非spring版本。
 * @author zhangdapeng
 * @version 1.0,2014-1-03 13:05:06
 * @since   1.0
 */
@Path("/file")
public class Upload {
	@Context
	HttpServletRequest request;
	@Context
	HttpServletResponse response;

	@POST
	@Path("/upload")
	@Consumes("multipart/form-data")
	public Response upLoad() throws Exception {
		String upload_file_path=request.getSession().getServletContext().getRealPath("/")+"upload/" ;
		System.out.println(upload_file_path);
		// 设置工厂
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置文件存储位置
		factory.setRepository(Paths.get(upload_file_path).toFile());
		// 设置大小,如果文件小于设置大小的话,放入内存中,如果大于的话则放入磁盘中,单位是byte
		factory.setSizeThreshold(1024 * 1024);

		ServletFileUpload upload = new ServletFileUpload(factory);
		// 这里就是中文文件名处理的代码,其实只有一行
		upload.setHeaderEncoding("utf-8");
		String fileName = null;
		List<FileItem> list = upload.parseRequest(request);
		for (FileItem item : list) {
			if (item.isFormField()) {
				String name = item.getFieldName();
				String value = item.getString("utf-8");
				System.out.println(name);
				System.out.println(value);
				request.setAttribute(name, value);
			} else {
				String name = item.getFieldName();
				String value = item.getName();
				System.out.println(name);
				System.out.println(value);

				fileName = Paths.get(value).getFileName().toString();
				request.setAttribute(name, fileName);
				if(!Paths.get(upload_file_path).toFile().exists()){
					Paths.get(upload_file_path).toFile().mkdirs();
				}
				// 写文件到path目录,文件名问filename
				item.write(new File(upload_file_path, fileName));
			}
		}

		return Response.status(200).entity("uploadFile is called, Uploaded file name : " + fileName).build();
	}

}






评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值