博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Code Examples for javax.servlet.http.Part
阅读量:6500 次
发布时间:2019-06-24

本文共 12723 字,大约阅读时间需要 42 分钟。

http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part

The following are 20 Jave code examples that show how to use the javax.servlet.http.Part class. These examples are extracted from open source projects. You can click  to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks! 

Example 1

  8 
vote

From project spring-framework, under directory /spring-web/src/main/java/org/springframework/web/multipart/support/, in source file StandardMultipartHttpServletRequest.java

 

public String getMultipartContentType(String paramOrFileName){ try { Part part=getPart(paramOrFileName); return (part != null ? part.getContentType() : null); } catch ( Exception ex) { throw new MultipartException("Could not access multipart servlet request",ex); } }

Example 2

  8 
vote

From project spring-framework, under directory /spring-web/src/main/java/org/springframework/web/multipart/support/, in source file StandardMultipartHttpServletRequest.java

 

public HttpHeaders getMultipartHeaders(String paramOrFileName){ try { Part part=getPart(paramOrFileName); if (part != null) { HttpHeaders headers=new HttpHeaders(); for ( String headerName : part.getHeaderNames()) { headers.put(headerName,new ArrayList
(part.getHeaders(headerName))); } return headers; } else { return null; } } catch ( Exception ex) { throw new MultipartException("Could not access multipart servlet request",ex); } }

Example 3

  7 
vote

From project ohmageServer, under directory /src/org/ohmage/request/, in source file Request.java

 

/**  * Reads the HttpServletRequest for a key-value pair and returns the value where the key is equal to the given key. * @param httpRequest A "multipart/form-data" request that contains the parameter that has a key value 'key'. * @param key The key for the value we are after in the 'httpRequest'. * @return Returns null if there is no such key in the request or if, after reading the object, it has a length of 0. Otherwise, it returns the value associated with the key as a byte array. * @throws ServletException Thrown if the 'httpRequest' is not a "multipart/form-data" request. * @throws IOException Thrown if there is an error reading the value fromthe request's input stream. * @throws IllegalStateException Thrown if the entire request is largerthan the maximum allowed size for a  request or if the value of the requested key is larger than the maximum allowed  size for a single value. */protected byte[] getMultipartValue(HttpServletRequest httpRequest,String key) throws ValidationException { try { Part part=httpRequest.getPart(key); if (part == null) { return null; } InputStream partInputStream=part.getInputStream(); ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); byte[] chunk=new byte[4096]; int amountRead; while ((amountRead=partInputStream.read(chunk)) != -1) { outputStream.write(chunk,0,amountRead); } if (outputStream.size() == 0) { return null; } else { return outputStream.toByteArray(); } } catch ( ServletException e) { LOGGER.error("This is not a multipart/form-data POST.",e); setFailed(ErrorCode.SYSTEM_GENERAL_ERROR,"This is not a multipart/form-data POST which is what we expect for the current API call."); throw new ValidationException(e); } catch ( IOException e) { LOGGER.error("There was an error reading the message from the input stream.",e); setFailed(); throw new ValidationException(e); } }

Example 4

  7 
vote

From project Ohmage_Server_2, under directory /src/org/ohmage/request/, in source file Request.java

 

/**  * Reads the HttpServletRequest for a key-value pair and returns the value where the key is equal to the given key. * @param httpRequest A "multipart/form-data" request that contains the parameter that has a key value 'key'. * @param key The key for the value we are after in the 'httpRequest'. * @return Returns null if there is no such key in the request or if, after reading the object, it has a length of 0. Otherwise, it returns the value associated with the key as a byte array. * @throws ServletException Thrown if the 'httpRequest' is not a "multipart/form-data" request. * @throws IOException Thrown if there is an error reading the value fromthe request's input stream. * @throws IllegalStateException Thrown if the entire request is largerthan the maximum allowed size for a  request or if the value of the requested key is larger than the maximum allowed  size for a single value. */protected byte[] getMultipartValue(HttpServletRequest httpRequest,String key) throws ValidationException { try { Part part=httpRequest.getPart(key); if (part == null) { return null; } InputStream partInputStream=part.getInputStream(); ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); byte[] chunk=new byte[4096]; int amountRead; while ((amountRead=partInputStream.read(chunk)) != -1) { outputStream.write(chunk,0,amountRead); } if (outputStream.size() == 0) { return null; } else { return outputStream.toByteArray(); } } catch ( ServletException e) { LOGGER.error("This is not a multipart/form-data POST.",e); setFailed(ErrorCodes.SYSTEM_GENERAL_ERROR,"This is not a multipart/form-data POST which is what we expect for the current API call."); throw new ValidationException(e); } catch ( IOException e) { LOGGER.error("There was an error reading the message from the input stream.",e); setFailed(); throw new ValidationException(e); } }

Example 5

  6 
vote

From project Coffee-Framework, under directory /coffeeframework-core/src/main/java/coffee/, in source fileCoffeeRequestContext.java

 

/**  * @param part * @return */public String getParameterFilename(Part part){ for ( String cd : part.getHeader("content-disposition").split(";")) { if (cd.trim().startsWith("filename")) { String fileName=cd.substring(cd.indexOf('=') + 1).trim().replace("\"",""); fileName=fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1); return fileName; } } return null; }

Example 6

  5 
vote

From project capedwarf-blue, under directory /blobstore/src/main/java/org/jboss/capedwarf/blobstore/, in source fileJBossBlobstoreService.java

 

public void storeUploadedBlobs(HttpServletRequest request) throws IOException, ServletException { Map
map=new HashMap
(); Map
> map2=new HashMap
>(); for ( Part part : request.getParts()) { if (ServletUtils.isFile(part)) { BlobKey blobKey=storeUploadedBlob(part); String name=part.getName(); map.put(name,blobKey); List
list=map2.get(name); if (list == null) { list=new LinkedList
(); map2.put(name,list); } list.add(blobKey); } } request.setAttribute(UPLOADED_BLOBKEY_ATTR,map); request.setAttribute(UPLOADED_BLOBKEY_LIST_ATTR,map2); }

Example 7

  5 
vote

From project capedwarf-blue, under directory /blobstore/src/main/java/org/jboss/capedwarf/blobstore/, in source fileJBossBlobstoreService.java

 

private BlobKey storeUploadedBlob(Part part) throws IOException { JBossFileService fileService=getFileService(); AppEngineFile file=fileService.createNewBlobFile(part.getContentType(),ServletUtils.getFileName(part)); ReadableByteChannel in=Channels.newChannel(part.getInputStream()); try { FileWriteChannel out=fileService.openWriteChannel(file,true); try { IOUtils.copy(in,out); } finally { out.closeFinally(); } } finally { in.close(); } return fileService.getBlobKey(file); }

Example 8

  5 
vote

From project capedwarf-blue, under directory /common/src/main/java/org/jboss/capedwarf/common/servlet/, in source fileServletUtils.java

 

public static String getFileName(Part part){ String contentDisposition=part.getHeader("content-disposition"); for ( String token : contentDisposition.split(";")) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf('=') + 1).trim().replace("\"",""); } } return null; }

Example 9

  5 
vote

From project Coffee-Framework, under directory /coffeeframework-core/src/main/java/coffee/, in source fileCoffeeRequestContext.java

 

/**  * @throws ServletException * @throws IOException * @throws FileUploadException */public void parseMultiPartRequest() throws ServletException, IOException { params=new HashMap
(); for ( Part part : request.getParts()) { String filename=getParameterFilename(part); String fieldname=part.getName(); if (filename == null) { String fieldvalue=getValue(part); params.put(fieldname,fieldvalue); } else if (!filename.isEmpty()) { if (reachedMaxFileSize(part)) throw new IOException("MAX_FILE_SIZE_REACHED"); params.put(fieldname,part); } } }

Example 10

  5 
vote

From project Coffee-Framework, under directory /coffeeframework-core/src/main/java/coffee/, in source fileCoffeeRequestContext.java

 

/**  * @param part * @return * @throws IOException */public String getValue(Part part) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(part.getInputStream(),"UTF-8")); StringBuilder value=new StringBuilder(); char[] buffer=new char[1024]; for (int length=0; (length=reader.read(buffer)) > 0; ) { value.append(buffer,0,length); } return value.toString(); }

Example 11

  5 
vote

From project Coffee-Framework, under directory /coffeeframework-core/src/main/java/coffee/servlet/, in source fileJsonUploadServlet.java

 

public void doUpload(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { Collection
parts=request.getParts(); ArrayList
createdObjects=new ArrayList
(); for ( Part part : parts) { String fileName=getFileName(part); writeFile(part,fileName); try { createdObjects.add(saveFile(fileName,part)); } catch ( Exception e) { throw new ServletException("Can't save the file.",e); } } PrintWriter writer=response.getWriter(); writer.write(new Gson().toJson(createdObjects)); }

Example 12

  5 
vote

From project fileUploadServlet3JSF2, under directory /src/main/java/net/balusc/http/multipart/, in source fileMultipartMap.java

 

/**  * Global constructor. */private MultipartMap(HttpServletRequest multipartRequest,String location,boolean multipartConfigured) throws ServletException, IOException { multipartRequest.setAttribute(ATTRIBUTE_NAME,this); this.encoding=multipartRequest.getCharacterEncoding(); if (this.encoding == null) { multipartRequest.setCharacterEncoding(this.encoding=DEFAULT_ENCODING); } this.location=location; this.multipartConfigured=multipartConfigured; for ( Part part : multipartRequest.getParts()) { String filename=getFilename(part); if (filename == null) { processTextPart(part); } else if (!filename.isEmpty()) { processFilePart(part,filename); } } }

Example 13

  5 
vote

From project fileUploadServlet3JSF2, under directory /src/main/java/net/balusc/http/multipart/, in source fileMultipartMap.java

 

/**  * Returns the filename from the content-disposition header of the given part. */private String getFilename(Part part){ for ( String cd : part.getHeader(CONTENT_DISPOSITION).split(";")) { if (cd.trim().startsWith(CONTENT_DISPOSITION_FILENAME)) { return cd.substring(cd.indexOf('=') + 1).trim().replace("\"",""); } } return null; }

Example 14

  5 
vote

From project fileUploadServlet3JSF2, under directory /src/main/java/net/balusc/http/multipart/, in source fileMultipartMap.java

 

/**  * Process given part as Text part. */private void processTextPart(Part part) throws IOException { String name=part.getName(); String[] values=(String[])super.get(name); if (values == null) { put(name,new String[]{ getValue(part)}); } else { int length=values.length; String[] newValues=new String[length + 1]; System.arraycopy(values,0,newValues,0,length); newValues[length]=getValue(part); put(name,newValues); } }

Example 15

  5 
vote

From project fileUploadServlet3JSF2, under directory /src/main/java/net/balusc/http/multipart/, in source fileMultipartMap.java

 

/**  * Process given part as File part which is to be saved in temp dir with the given filename. */private void processFilePart(Part part,String filename) throws IOException { filename=filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); String prefix=filename; String suffix=""; if (filename.contains(".")) { prefix=filename.substring(0,filename.lastIndexOf('.')); suffix=filename.substring(filename.lastIndexOf('.')); } File file=File.createTempFile(prefix + "_",suffix,new File(location)); if (multipartConfigured) { part.write(file.getName()); } else { InputStream input=null; OutputStream output=null; try { input=new BufferedInputStream(part.getInputStream(),DEFAULT_BUFFER_SIZE); output=new BufferedOutputStream(new FileOutputStream(file),DEFAULT_BUFFER_SIZE); byte[] buffer=new byte[DEFAULT_BUFFER_SIZE]; for (int length=0; ((length=input.read(buffer)) > 0); ) { output.write(buffer,0,length); } } finally

转载地址:http://zcvyo.baihongyu.com/

你可能感兴趣的文章
理解:思科设备上的网络地址翻译功能(NAT)功能
查看>>
演示:使用协议分析器取证IPv6的报文结构
查看>>
oracle 11gr2 rac中的4种IP解说
查看>>
为什么你找不到工作?
查看>>
一名合格的测试员应具备的素质
查看>>
SCCM 2007系列3 配置
查看>>
Lync 小技巧-22-申请属于自己的域名
查看>>
20 个免费的 jQuery 的工具提示插件:
查看>>
交易算法故障导致Knight资本集团损失超过4亿美元_IT新闻_博客园
查看>>
linux改ip
查看>>
Oracle数据表解锁
查看>>
堆,栈,new/delete/malloc/free[zz]
查看>>
The CATALINA_HOME environment variable is not defined correctly
查看>>
关于vs2010 起始页
查看>>
VC:CEdit类
查看>>
PacketFence 3.6.0 发布,网络接入控制
查看>>
Debian Linux下的Python学习——列表,元组和字典之列表
查看>>
从sprintf函数谈符号扩展问题
查看>>
Android加载对话框,异步执行代码的封装类
查看>>
远程管理server core上的hyper-v
查看>>