本文整理匯總了Java中org.apache.commons.fileupload.FileUpload.parseRequest方法的典型用法代碼示例。如果您正苦於以下問題:Java FileUpload.parseRequest方法的具體用法?Java FileUpload.parseRequest怎麽用?Java FileUpload.parseRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.fileupload.FileUpload
的用法示例。
在下文中一共展示了FileUpload.parseRequest方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: extractAttachments
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AttachmentIsEmptyException
* Thrown, when the attachment is of zero size.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws FileUploadException, IOException,
AttachmentIsEmptyException, AuthorizationException {
List<AttachmentResource> result = new ArrayList<>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例2: extractAttachments
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AttachmentIsEmptyException
* Thrown, when the attachment is of zero size.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AttachmentIsEmptyException, AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例3: extractAttachments
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例4: extractAttachments
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.checkAttachmentSize(file);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例5: handleFileUpload
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* Handles a file contained in the specified {@code request}. If you use
* FileUpload, please make sure to add the needed dependencies, i.e.
* {@code commons-fileupload} and {@code servlet-api} (later will be removed
* in a newer version of the {@code commons-fileupload}.
*
* @param request
* the request to handle the file upload from
* @param factory
* the {@code FileItemFactory} used to handle the request
*
* @return the {@code List} of loaded files
*
* @throws FileUploadException
* if the file upload fails
*/
public static List<FileItem> handleFileUpload(final HttpRequest request,
final FileItemFactory factory) throws FileUploadException {
final List<FileItem> items;
if (request instanceof HttpEntityEnclosingRequest
&& "POST".equals(request.getRequestLine().getMethod())) {
// create the ServletFileUpload
final FileUpload upload = new FileUpload(factory);
final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
items = upload.parseRequest(new RequestWrapper(entityRequest));
} else {
items = new ArrayList<FileItem>();
}
return items;
}
示例6: uploadFiles
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
/**
* @see http://www.oschina.net/code/snippet_698737_13402
* @param request
* @return
* @throws IOException
*/
public static Map<String, Object> uploadFiles(HttpServlet servlet, HttpServletRequest request) {
Map<String, Object> map = JwUtils.newHashMap();
Map<String, String> fileMap = JwUtils.newHashMap();
map.put("file", fileMap);
DiskFileItemFactory factory = new DiskFileItemFactory();// 創建工廠
factory.setSizeThreshold(1024 * 1024 * 30);// 設置最大緩衝區為30M
// 設置緩衝區目錄
String savePath = servlet.getServletContext().getRealPath("/WEB-INF/temp");
factory.setRepository(new File(savePath));
FileUpload upload = new FileUpload(factory);// 獲得上傳解析器
upload.setHeaderEncoding("UTF-8");// 解決上傳文件名亂碼
try {
String targetFolderPath = servlet.getServletContext().getRealPath("/WEB-INF/" + ConfigUtils.getProperty("web.files.upload.folder"));
File targetFolder = new File(targetFolderPath);// 目標文件夾
if(!targetFolder.exists()) {
targetFolder.mkdir();
}
List<FileItem> fileItems = upload.parseRequest(new ServletRequestContext(request));// 解析請求體
for (FileItem fileItem : fileItems) {
if (fileItem.isFormField()) {// 判斷是普通表單項還是文件上傳項
String name = fileItem.getFieldName();// 表單名
String value = fileItem.getString("UTF-8");// 表單值
map.put(name, value);
} else {// 文件上傳項
String fileName = fileItem.getName();// 獲取文件名
if (StringUtils.isEmpty(fileName))// 判斷是否上傳了文件
continue;
// 截取文件名
int index = fileName.lastIndexOf("/");
if (index > -1) {
fileName = fileName.substring(index);
}
// 檢查文件是否允許上傳
index = fileName.lastIndexOf(".");
if (index > -1 && index < fileName.length() - 1) {
String ext = fileName.substring(index + 1).toLowerCase();
if (!ConfigUtils.getString("web.files.upload.extension").contains(";" + ext + ";")) {
LOGGER.warn("The file {} is not allowed to upload.", fileName);
continue;
}
}
// 生成唯一文件名,保留原文件名
String newFileName = UUID.randomUUID().toString();
// 將文件內容寫到服務器端
String targetPath = targetFolderPath + "/" + newFileName;
File targetFile = new File(targetPath);// 目標文件
targetFile.createNewFile();
fileItem.write(targetFile);
fileItem.delete();// 刪除臨時文件
fileMap.put(fileName, newFileName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
示例7: getFileUploadContent
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
public static ClassifiableContentIF getFileUploadContent(HttpServletRequest request) {
// Handle file upload
String contentType = request.getHeader("content-type");
// out.write("CT: " + contentType + " " + tm + " " + id);
if (contentType != null && contentType.startsWith("multipart/form-data")) {
try {
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
for (FileItem item : upload.parseRequest(request)) {
if (item.getSize() > 0) {
// ISSUE: could make use of content type if known
byte[] content = item.get();
ClassifiableContent cc = new ClassifiableContent();
String name = item.getName();
if (name != null)
cc.setIdentifier("fileupload:name:" + name);
else
cc.setIdentifier("fileupload:field:" + item.getFieldName());
cc.setContent(content);
return cc;
}
}
} catch (Exception e) {
throw new OntopiaRuntimeException(e);
}
}
return null;
}
示例8: writeMultipart
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
@Test
public void writeMultipart() throws Exception {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name 1", "value 1");
parts.add("name 2", "value 2+1");
parts.add("name 2", "value 2+2");
parts.add("name 3", null);
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
parts.add("logo", logo);
// SPR-12108
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
@Override
public String getFilename() {
return "Hall\u00F6le.jpg";
}
};
parts.add("utf8", utf8);
Source xml = new StreamSource(new StringReader("<root><child/></root>"));
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(MediaType.TEXT_XML);
HttpEntity<Source> entity = new HttpEntity<Source>(xml, entityHeaders);
parts.add("xml", entity);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setMultipartCharset(UTF_8);
this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);
final MediaType contentType = outputMessage.getHeaders().getContentType();
assertNotNull("No boundary found", contentType.getParameter("boundary"));
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(fileItemFactory);
RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
List<FileItem> items = fileUpload.parseRequest(requestContext);
assertEquals(6, items.size());
FileItem item = items.get(0);
assertTrue(item.isFormField());
assertEquals("name 1", item.getFieldName());
assertEquals("value 1", item.getString());
item = items.get(1);
assertTrue(item.isFormField());
assertEquals("name 2", item.getFieldName());
assertEquals("value 2+1", item.getString());
item = items.get(2);
assertTrue(item.isFormField());
assertEquals("name 2", item.getFieldName());
assertEquals("value 2+2", item.getString());
item = items.get(3);
assertFalse(item.isFormField());
assertEquals("logo", item.getFieldName());
assertEquals("logo.jpg", item.getName());
assertEquals("image/jpeg", item.getContentType());
assertEquals(logo.getFile().length(), item.getSize());
item = items.get(4);
assertFalse(item.isFormField());
assertEquals("utf8", item.getFieldName());
assertEquals("Hall\u00F6le.jpg", item.getName());
assertEquals("image/jpeg", item.getContentType());
assertEquals(logo.getFile().length(), item.getSize());
item = items.get(5);
assertEquals("xml", item.getFieldName());
assertEquals("text/xml", item.getContentType());
verify(outputMessage.getBody(), never()).close();
}
示例9: writeMultipartOrder
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
@Test
public void writeMultipartOrder() throws Exception {
MyBean myBean = new MyBean();
myBean.setString("foo");
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("part1", myBean);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(MediaType.TEXT_XML);
HttpEntity<MyBean> entity = new HttpEntity<MyBean>(myBean, entityHeaders);
parts.add("part2", entity);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.setMultipartCharset(UTF_8);
this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);
final MediaType contentType = outputMessage.getHeaders().getContentType();
assertNotNull("No boundary found", contentType.getParameter("boundary"));
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(fileItemFactory);
RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
List<FileItem> items = fileUpload.parseRequest(requestContext);
assertEquals(2, items.size());
FileItem item = items.get(0);
assertTrue(item.isFormField());
assertEquals("part1", item.getFieldName());
assertEquals("{\"string\":\"foo\"}", item.getString());
item = items.get(1);
assertTrue(item.isFormField());
assertEquals("part2", item.getFieldName());
// With developer builds we get: <MyBean><string>foo</string></MyBean>
// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
// So... we make a compromise:
assertThat(item.getString(),
allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}
示例10: execute
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
Logger logger = MiscUtils.getLogger();
String simulationData = null;
boolean simulationError = false;
try {
FileUpload upload = new FileUpload(new DefaultFileItemFactory());
@SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(request);
for(FileItem item:items) {
if(item.isFormField()) {
String name = item.getFieldName();
if(name.equals("simulateError")) {
simulationError=true;
}
} else {
if(item.getFieldName().equals("simulateFile")) {
InputStream is = item.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
simulationData = writer.toString();
}
}
}
if(simulationData != null && simulationData.length()>0) {
if(simulationError) {
Driver.readResponseFromXML(request, simulationData);
simulationData = (String)request.getAttribute("olisResponseContent");
request.getSession().setAttribute("errors",request.getAttribute("errors"));
}
request.getSession().setAttribute("olisResponseContent", simulationData);
request.setAttribute("result", "File successfully uploaded");
}
}catch(Exception e) {
MiscUtils.getLogger().error("Error",e);
}
return mapping.findForward("success");
}
示例11: writeMultipart
import org.apache.commons.fileupload.FileUpload; //導入方法依賴的package包/類
@Test
public void writeMultipart() throws Exception {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("name 1", "value 1");
parts.add("name 2", "value 2+1");
parts.add("name 2", "value 2+2");
parts.add("name 3", null);
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
parts.add("logo", logo);
Source xml = new StreamSource(new StringReader("<root><child/></root>"));
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(MediaType.TEXT_XML);
HttpEntity<Source> entity = new HttpEntity<Source>(xml, entityHeaders);
parts.add("xml", entity);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(parts, MediaType.MULTIPART_FORM_DATA, outputMessage);
final MediaType contentType = outputMessage.getHeaders().getContentType();
assertNotNull("No boundary found", contentType.getParameter("boundary"));
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
FileUpload fileUpload = new FileUpload(fileItemFactory);
List items = fileUpload.parseRequest(new MockHttpOutputMessageRequestContext(outputMessage));
assertEquals(5, items.size());
FileItem item = (FileItem) items.get(0);
assertTrue(item.isFormField());
assertEquals("name 1", item.getFieldName());
assertEquals("value 1", item.getString());
item = (FileItem) items.get(1);
assertTrue(item.isFormField());
assertEquals("name 2", item.getFieldName());
assertEquals("value 2+1", item.getString());
item = (FileItem) items.get(2);
assertTrue(item.isFormField());
assertEquals("name 2", item.getFieldName());
assertEquals("value 2+2", item.getString());
item = (FileItem) items.get(3);
assertFalse(item.isFormField());
assertEquals("logo", item.getFieldName());
assertEquals("logo.jpg", item.getName());
assertEquals("image/jpeg", item.getContentType());
assertEquals(logo.getFile().length(), item.getSize());
item = (FileItem) items.get(4);
assertEquals("xml", item.getFieldName());
assertEquals("text/xml", item.getContentType());
verify(outputMessage.getBody(), never()).close();
}