java 文件上传和下载处理

12 篇文章 0 订阅
8 篇文章 0 订阅
Util.java
	public static void isExist(String path) {  
		File file = new File(path);  
		//判断文件夹是否存在,如果不存在则创建文件夹  
		if (!file.exists()){   
			file.mkdirs();  
		} 
	}

文件上传下载处理

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.bio.framework.util.common.ImageUtil;
import com.bio.web.svc.model.SvcApply;
import com.bio.web.svc.model.SvcProject;
import com.bio.web.svc.model.SvcResultDocs;
import com.bio.web.svc.service.SvcApplyManager;
import com.bio.web.svc.service.SvcProjectManager;
import com.bio.web.svc.service.SvcResultDocsManager;

@Controller
@RequestMapping(value = "/backoffice")
public class UploadFileController {
	@Autowired
	private SvcResultDocsManager svcResultDocsManager;
	@Autowired
	private SvcApplyManager svcApplyManager;
	@Autowired
	private SvcProjectManager svcProjectManager;

	@Value("#{applicationProperties[dataFiles]}")
	private String dataFiles;

	/**
	 * 文件上传
	 **/
	@RequestMapping(value = "/UploadFile/uploadfileSave", method = RequestMethod.POST)
	public void uploadfileSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer applyId = Integer.parseInt(request.getParameter("applyId"));
		SvcApply svcapply = svcApplyManager.getById(applyId);
		SvcProject svcProject = svcProjectManager.getById(svcapply.getProjectId());
		//拼接路径
		String path = dataFiles + "member-file" + File.separatorChar + svcapply.getMemberId() + File.separatorChar
				+ "service-file" + File.separatorChar + "unitinfo-file" + File.separatorChar
				+ svcProject.getUnitinfoId();
		//判断是否存储路径,不存在则创建
		ImageUtil.isExist(path);
		String fileName = request.getParameter("Filename");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile filedata = (MultipartFile) multipartRequest.getFile("Filedata");
		if (filedata != null) {
			Calendar cal = Calendar.getInstance();
			Date date = cal.getTime();
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String myTime = format.format(date);

			String typ = fileName.substring(fileName.lastIndexOf("."));

			String pathA = path + File.separatorChar + myTime + typ;

			File file = new File(pathA);
			filedata.transferTo(file);
			svcResultDocsManager.saveSvcResultDocs(applyId, fileName, pathA);
		}
	}

	/**
	 * 文件下载
	 **/
	@RequestMapping(value = "/UploadFile/downloadFile", method = RequestMethod.GET)
	public void downloadFile(HttpServletRequest request, HttpServletResponse response, Integer fileId)
			throws Exception {
		SvcResultDocs svcResultDocs = svcResultDocsManager.getById(fileId);
		// 获得请求文件名
		String filepath = svcResultDocs.getFilepath();
		String filename = URLEncoder.encode(svcResultDocs.getFilenm(), "UTF-8");
		if (filename.length() > 150) { // 解决IE 6.0 bug
			filename = new String(svcResultDocs.getFilenm().getBytes("GBK"), "ISO-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + filename);

		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(filepath));
			bos = new BufferedOutputStream(response.getOutputStream());

			byte[] buff = new byte[2048];
			int bytesRead;

			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}

		} catch (final IOException e) {
			System.out.println("IOException." + e);

		} finally {
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}

}

<a href="${ctx}/backoffice/UploadFile/downloadFile.do?fileId=${item.id}"><spring:message code="common.label.download" text="下载"/></a>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值