本文整理汇总了Java中org.apache.commons.fileupload.util.Streams.asString方法的典型用法代码示例。如果您正苦于以下问题:Java Streams.asString方法的具体用法?Java Streams.asString怎么用?Java Streams.asString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.fileupload.util.Streams
的用法示例。
在下文中一共展示了Streams.asString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processFileItemStreamAsFormField
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* Processes the FileItemStream as a Form Field.
*
* @param itemStream file item stream
*/
private void processFileItemStreamAsFormField(FileItemStream itemStream) {
String fieldName = itemStream.getFieldName();
try {
List<String> values;
String fieldValue = Streams.asString(itemStream.openStream());
if (!parameters.containsKey(fieldName)) {
values = new ArrayList<>();
parameters.put(fieldName, values);
} else {
values = parameters.get(fieldName);
}
values.add(fieldValue);
} catch (IOException e) {
LOG.warn("Failed to handle form field '{}'.", fieldName, e);
}
}
示例2: getRequestData
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
private Map<String, String> getRequestData(HttpServletRequest request) {
Map<String, String> requestData = new HashMap<>();
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator itemIterator = upload.getItemIterator(request);
while (itemIterator.hasNext()) {
FileItemStream item = itemIterator.next();
InputStream itemStream = item.openStream();
String value = Streams.asString(itemStream, CharEncoding.UTF_8);
requestData.put(item.getFieldName(), value);
}
} catch (FileUploadException | IOException e) {
LOGGER.error("Failed to process request", e);
}
return requestData;
}
示例3: asString
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* @param stream
* The ServletFileUpload input stream
* @param commonsConfigurator
* Used to get the password for encryption
*/
public static String asString(InputStream stream,
CommonsConfigurator commonsConfigurator)
throws IOException {
String value = Streams.asString(stream);
if (isEncrypted(value, commonsConfigurator)) {
try {
value = StringUtils.substringAfter(value, Pbe.KAWANFW_ENCRYPTED);
value = new Pbe().decryptFromHexa(value,
CommonsConfiguratorCall.getEncryptionPassword(commonsConfigurator));
return value;
} catch (Exception e) {
String message = Tag.PRODUCT_USER_CONFIG_FAIL
+ " Impossible to decrypt the value " + value;
message += ". Check that password values are the same on client and server side.";
throw new IOException(message, e);
}
} else {
return value;
}
}
示例4: parse
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
public void parse(MultipartRequestCallback callback) throws IOException, FileUploadException, StatusServletException {
if (!ServletFileUpload.isMultipartContent(request)) {
LOGGER.error("Request content is not multipart.");
throw new StatusServletException(Response.SC_PRECONDITION_FAILED);
}
final FileItemIterator iterator = new ServletFileUpload(new DiskFileItemFactory()).getItemIterator(request);
while (iterator.hasNext()) {
// Gets the first HTTP request element.
final FileItemStream item = iterator.next();
if (item.isFormField()) {
final String value = Streams.asString(item.openStream(), "UTF-8");
properties.put(item.getFieldName(), value);
} else if(callback != null) {
callback.onInputStream(item.openStream(), item.getFieldName(), item.getContentType());
}
}
}
示例5: handleMultipart
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
private String handleMultipart(RecordedRequest request) {
RecordedUpload upload = new RecordedUpload(request);
Exception exception;
try {
Map<String,String> params = new HashMap<>();
FileItemIterator iter = upload.getItemIterator();
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream);
System.out.println("Form field " + name + " with value "
+ value + " detected.");
params.put(name,value);
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
params.put(name, "file->"+item.getName());
}
}
return "Multipart:"+JSON.toJSONString(params);
} catch (Exception e) {
exception = e;
}
return "Multipart:error->"+exception;
}
示例6: readFormData
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
/**
* Reads form values from the multipart request until a file is encountered. Field values are stored as strings for
* retrieval using {@link #getFormFieldValue}.
* @return True if there is an upload file available to read via {@link #getUploadFileItem()}.
*/
public boolean readFormData() {
mUploadFileItem = null;
try {
while (mItemIterator.hasNext()) {
FileItemStream lCurrentItem = mItemIterator.next();
/**
* NOTE: the InputStream here is read here in a blocking way. Long upload hangs have been observed on live
* environments at this point due to network issues. It should be possible to convert the stream to a
* non-blocking stream at this point if required.
*/
InputStream lItemInputStream = lCurrentItem.openStream();
if (lCurrentItem.isFormField()) {
//Read form values into the map
String lParamName = lCurrentItem.getFieldName();
String lFieldValue = Streams.asString(lItemInputStream);
mFieldParamMap.put(lParamName, lFieldValue);
}
else {
//We've hit the file field, so stop the read for now
mUploadFileItem = lCurrentItem;
break;
}
lItemInputStream.close();
}
}
catch (IOException | FileUploadException e) {
throw new ExInternal("Failed to read form data for the multipart request", e);
}
return mUploadFileItem != null;
}
示例7: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
long gameId = 0l;
String auth = null;
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
String json = "";
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream);
if ("gameId".equals(name)) gameId = Long.parseLong(value);
if ("auth".equals(name)) auth = value;
} else {
json = Streams.asString(stream);
}
}
res.setContentType("text/plain");
JSONObject jObject = new JSONObject(json);
Object deserialized = JsonBeanDeserializer.deserialize(json);
if (deserialized instanceof GamePackage && ((GamePackage) deserialized).getGame() != null)
unpackGame((GamePackage) deserialized, req, auth);
if (deserialized instanceof RunPackage && ((RunPackage) deserialized ).getRun() != null)
unpackRun((RunPackage) deserialized, req, gameId, auth);
} catch (Exception ex) {
throw new ServletException(ex);
}
}
示例8: internalImport
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void internalImport(
HttpServletRequest request,
Properties options,
long projectID
) throws Exception {
String url = null;
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName().toLowerCase();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (name.equals("url")) {
url = Streams.asString(stream);
} else {
options.put(name, Streams.asString(stream));
}
} else {
String fileName = item.getName().toLowerCase();
try {
ProjectManager.getSingleton().importProject(projectID, stream, !fileName.endsWith(".tar"));
} finally {
stream.close();
}
}
}
if (url != null && url.length() > 0) {
internalImportURL(request, options, projectID, url);
}
}
示例9: getFileItem
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
private void getFileItem(HttpServletRequest request, File root) throws FileUploadException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
throw new IllegalArgumentException("Not multipart...");
}
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
String fileName = null;
String path = null;
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
System.out.println("Name=" + item.getName());
if (item.isFormField()) {
String value = Streams.asString(stream);
System.out.println("FormField " + name + "=" + value);
if (name.equals("name")) {
fileName = value;
}
if (name.equals("path")) {
path = value;
}
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
File output = new File(root, path + "/" + fileName);
System.out.println("Write upload to " + output.getPath());
IOUtil.copyCompletely(stream, new FileOutputStream(output));
}
}
}
示例10: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try {
Long runId = null;
String account = null;
String serverPath = null;
boolean last = false;
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
System.out.println("before while");
while (iterator.hasNext()) {
System.out.println("in while");
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
if ("runId".equals(item.getFieldName())) {
runId = Long.parseLong(Streams.asString(stream));
System.out.println("runid is " + runId);
}
if ("account".equals(item.getFieldName())) {
account = Streams.asString(stream);
System.out.println("account is " + account);
}
if ("last".equals(item.getFieldName())) {
last = Boolean.parseBoolean(Streams.asString(stream));
System.out.println("last is " + last);
}
if ("serverPath".equals(item.getFieldName())) {
serverPath = Streams.asString(stream);
System.out.println("serverPath is " + serverPath);
}
} else { log.warning("Got an uploaded file: " + item.getFieldName() + ", name = " + item.getName());
AppEngineFile file = storeBlob(item.getContentType(), item.getName(), stream, last, serverPath);
BlobKey blobkey = fileService.getBlobKey(file);
if (blobkey != null) {
// File exists
BlobKey oldkey = FilePathManager.getBlobKey(account, runId, item.getName());
if (oldkey != null) {
FilePathManager.delete(oldkey);
blobstoreService.delete(oldkey);
}
FilePathManager.addFile(runId, account, item.getName(), blobkey);
System.out.println(blobkey.toString());
}
res.getWriter().write(file.getFullPath());
// else {
// blobkey.toString();
// }
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
示例11: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try {
Long runId = null;
String account = null;
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
if ("runId".equals(item.getFieldName())) {
runId = Long.parseLong(Streams.asString(stream));
}
if ("account".equals(item.getFieldName())) {
account = Streams.asString(stream);
}
} else {
BlobKey blobkey = storeBlob(item.getContentType(), item.getName(), stream);
if (blobkey != null) {
System.out.println(blobkey);
// File exists
BlobKey oldkey = FilePathManager.getBlobKey(account, runId, item.getName());
if (oldkey != null) {
FilePathManager.delete(oldkey);
blobstoreService.delete(oldkey);
}
FilePathManager.addFile(runId, account, item.getName(), blobkey);
} else {
blobkey.toString();
}
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
示例12: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//log.info("Request:" + path);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
ServletFileUpload upload = new ServletFileUpload();
String user = request.getParameter("user");
String dir = request.getParameter("dir");
String path = request.getParameter("path");
String client = request.getRemoteAddr().replace('.', '_');
Map<String, String> meta = new HashMap<String, String>();
String archivePath = null;
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
InputStream stream = item.openStream();
if(item.getFieldName().equals("user")){
user = Streams.asString(stream);
}else if(item.getFieldName().equals("dir")){
dir = Streams.asString(stream);
}else if(item.getFieldName().equals("path")){
path = Streams.asString(stream);
}else if(item.getFieldName().startsWith("meta_")) {
meta.put(item.getFieldName(), Streams.asString(stream));
}
stream.close();
} else if(!item.getName().equals("")) {
archivePath = getArchivePath(user, dir, path);
processUploadedFile(item, archivePath, client, meta);
//request.setAttribute("message", "Update ok!");
response.setHeader("upload_status", "ok");
response.setHeader("archive_path", archivePath);
}
}
} catch (Exception e) {
//request.setAttribute("message", "Failed to uploading file, error:" + e.toString());
response.setHeader("upload_status", e.toString());
log.error(e.toString(), e);
}
}else {
log.warn("The request is not a multpart content type.");
}
doGet(request, response);
}
示例13: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//log.info("Request:" + path);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
ServletFileUpload upload = new ServletFileUpload();
String name = request.getParameter("name");
String path = request.getParameter("path");
String client = request.getRemoteAddr().replace('.', '_');
Map<String, String> meta = new HashMap<String, String>();
String archivePath = null;
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (item.isFormField()) {
InputStream stream = item.openStream();
if(item.getFieldName().equals("name")){
name = Streams.asString(stream);
}else if(item.getFieldName().equals("path")){
path = Streams.asString(stream);
}else if(item.getFieldName().startsWith("meta_")) {
meta.put(item.getFieldName(), Streams.asString(stream));
}
stream.close();
} else if(!item.getName().equals("")) {
archivePath = getArchivePath(name, path);
processUploadedFile(item, archivePath, client, meta);
//request.setAttribute("message", "Update ok!");
response.setHeader("upload_status", "ok");
response.setHeader("archive_path", archivePath);
response.setHeader("uuid", "ok");
}
}
} catch (Exception e) {
//request.setAttribute("message", "Failed to uploading file, error:" + e.toString());
response.setHeader("upload_status", e.toString());
log.error(e.toString(), e);
}
}else {
log.warn("The request is not a multpart content type.");
}
doGet(request, response);
}
示例14: doPost
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException
{
ClientRequestIF clientRequest = (ClientRequestIF)req.getAttribute(ClientConstants.CLIENTREQUEST);
// capture the session id
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart)
{
// TODO Change exception type
String msg = "The HTTP Request must contain multipart content.";
throw new RuntimeException(msg);
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload();
upload.setFileItemFactory(factory);
try
{
// Parse the request
FileItemIterator iter = upload.getItemIterator(req);
String fileName = null;
String extension = null;
InputStream stream = null;
String uploadPath = null;
while(iter.hasNext())
{
FileItemStream item = iter.next();
InputStream input = item.openStream();
if(item.isFormField() && item.getFieldName().equals(WEB_FILE_UPLOAD_PATH_FIELD_NAME))
{
uploadPath = Streams.asString(input);
}
else if (!item.isFormField())
{
String fullName = item.getName();
int extensionInd = fullName.lastIndexOf(".");
fileName = fullName.substring(0, extensionInd);
extension = fullName.substring(extensionInd + 1);
stream = input;
}
}
if (stream != null)
{
clientRequest.newFile(uploadPath, fileName, extension, stream);
}
}
catch (FileUploadException e)
{
throw new FileWriteExceptionDTO(e.getLocalizedMessage());
}
}
示例15: InterceptingHTTPServletRequest
import org.apache.commons.fileupload.util.Streams; //导入方法依赖的package包/类
public InterceptingHTTPServletRequest(HttpServletRequest request) throws FileUploadException, IOException {
super(request);
allParameters = new Vector<Parameter>();
allParameterNames = new Vector<String>();
/*
* Get all the regular parameters.
*/
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()) {
String param = (String)e.nextElement();
allParameters.add(new Parameter(param,request.getParameter(param),false));
allParameterNames.add(param);
}
/*
* Get all the multipart fields.
*/
isMultipart = ServletFileUpload.isMultipartContent(request);
if ( isMultipart ) {
requestBody = new RandomAccessFile( File.createTempFile("oew","mpc"), "rw");
byte buffer[] = new byte[CHUNKED_BUFFER_SIZE];
long size = 0;
int len = 0;
while ( len != -1 && size <= Integer.MAX_VALUE) {
len = request.getInputStream().read(buffer, 0, CHUNKED_BUFFER_SIZE);
if ( len != -1 ) {
size += len;
requestBody.write(buffer,0,len);
}
}
is = new RAFInputStream(requestBody);
ServletFileUpload sfu = new ServletFileUpload();
FileItemIterator iter = sfu.getItemIterator(this);
while(iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
/*
* If this is a regular form field, add it to our
* parameter collection.
*/
if (item.isFormField()) {
String value = Streams.asString(stream);
allParameters.add(new Parameter(name,value,true));
allParameterNames.add(name);
} else {
/*
* This is a multipart content that is not a
* regular form field. Nothing to do here.
*/
}
}
requestBody.seek(0);
}
}