读书人

下传上载文件-项目中的例子

发布时间: 2012-12-18 12:43:41 作者: rapoo

上传下载文件--项目中的例子

//*************************************************上传和下载文件begin
???
??? private static final String FILE_ENCODEING = "UTF-8";
??? private static final String CONTENT_TYPE = "application/octet-stream";
???
??? /**
??? ?* 上传文件
??? ?* @param obj
??? ?*/
??? public void upload(File upload,String fileName)throws Exception{
??? ??? TbSysAttach immpSellAttach = new? TbSysAttach();
??? ??? byte[] attachContent = getBytesFromFile(upload);
??? ??? immpSellAttach.setAttachContent(attachContent);
??? ??? immpSellAttach.setStatusId("0"); //有效标记
??? ??? immpSellAttach.setAttachName(fileName);
??? ??? sysmngHibernate.saveObj(immpSellAttach);
??? }
??? /**
???? * 文件转化为字节数组
???? *
???? *?
???? */
??? public static byte[] getBytesFromFile(File f){
??????? if (f == null) {
??????????? return null;
??????? }
??????? try{
??????? ??? long starttime = System.currentTimeMillis();
??????????? FileInputStream stream = new FileInputStream(f);
??????????? ByteArrayOutputStream out = new ByteArrayOutputStream(100000);
??????????? //ByteArrayInputStream ins = new ByteArrayInputStream(stream);
??????????? byte[] b = new byte[100000];
??????????? int n;
??????????? int i=0;
??????????? while ((n = stream.read(b)) != -1){
??????????????? out.write(b, 0, n);
??????????????? System.out.println(i++);
??????????????? System.out.println(n);
??????????? }
??????????? System.out.println("------"+b.length);
//??????????? byte[] aimBarry = new byte[];
//??????????? ByteArrayInputStream ins = new ByteArrayInputStream(aimBarry);
???????????
???????????
???????????
??????????? stream.close();
??????????? out.close();
??????????? System.out.println("用时--》"+(System.currentTimeMillis() - starttime));
???????????
???????????
??????????? return out.toByteArray();
??????? } catch (IOException e){
??????? }
??????? return null;
??? }
???

???
??? /**
??? ?* 下载文件
??? ?* @param attachId
??? ?* @param request
??? ?* @param response
??? ?* @throws Exception
??? ?*/
??? public void downFile(String attachId,HttpServletRequest request,HttpServletResponse response)throws Exception
??? {
??? ??? String fileName = null;
??? ??? byte[] fileContent = null;
??? ??? TbSysAttach immpSellAttach = (TbSysAttach)sysmngHibernate.getSession().get(TbSysAttach.class, attachId);
??? ??? if(null!=immpSellAttach){
??? ??? ??? ?fileName = immpSellAttach.getAttachName();
??? ??? ??? ?fileContent = immpSellAttach.getAttachContent();
??? ??? }
??????? BufferedOutputStream bos;
??????? String downFileName = null;
??????? bos = null;
???????
??????? if(fileContent == null || fileContent.length < 0 || response == null)
??????? ??? return;
???????
??????? try
??????? {
??????? ??? response.setContentType(CONTENT_TYPE);
??????????? if(fileName == null || fileName.equals(""))
??????????????? fileName = "anonymous";
??????????? downFileName = URLEncoder.encode(fileName, FILE_ENCODEING);
??????????? if(request.getHeader("User-Agent").indexOf("MSIE 5.5") != -1)
??????????? ??? response.setHeader("Content-disposition", (new StringBuilder("filename=")).append(downFileName).toString());
??????????? else
??????????? ??? response.setHeader("Content-disposition", (new StringBuilder("attachment; filename=")).append(downFileName).toString());
??????????? byte buff[] = fileContent;
??????????? bos = new BufferedOutputStream(response.getOutputStream());
??????????? bos.write(buff);
??????????? bos.flush();
??????? }
??????? catch(Exception ex)
??????? {
??????? ???
??????? }
??????? finally
??????? {
??????? ??? if(bos != null)
??????? ??? {
??????? ??? ??? bos.close();
??????? ??? }
??????? }
??? }

读书人网 >编程

热点推荐