本文整理汇总了Java中com.sun.jersey.multipart.file.FileDataBodyPart类的典型用法代码示例。如果您正苦于以下问题:Java FileDataBodyPart类的具体用法?Java FileDataBodyPart怎么用?Java FileDataBodyPart使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileDataBodyPart类属于com.sun.jersey.multipart.file包,在下文中一共展示了FileDataBodyPart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serialize
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON is supported for now).
*/
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException {
if (contentType.startsWith("multipart/form-data")) {
FormDataMultiPart mp = new FormDataMultiPart();
for (Entry<String, Object> param: formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE));
} else {
mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
}
}
return mp;
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
return this.getXWWWFormUrlencodedParams(formParams);
} else {
// We let Jersey attempt to serialize the body
return obj;
}
}
示例2: serialize
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
/**
* Serialize the given Java object into string according the given
* Content-Type (only JSON is supported for now).
*/
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException {
if (contentType.startsWith("multipart/form-data")) {
FormDataMultiPart mp = new FormDataMultiPart();
for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) {
File file = (File) param.getValue();
mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE));
} else {
mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
}
}
return mp;
} else if (contentType.startsWith("application/x-www-form-urlencoded")) {
return this.getXWWWFormUrlencodedParams(formParams);
} else {
// We let Jersey attempt to serialize the body
return obj;
}
}
示例3: sendComplexMessage
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
private ClientResponse sendComplexMessage(String recipient) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME
+ "/messages");
FormDataMultiPart formData = new FormDataMultiPart();
formData.field("from", "Mailgun User <[email protected]" + MAILGUN_DOMAIN_NAME + ">");
formData.field("to", recipient);
formData.field("subject", "Complex Mailgun Example");
formData.field("html", "<html>HTML <strong>content</strong></html>");
ClassLoader classLoader = getClass().getClassLoader();
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(ClientResponse.class, formData);
}
示例4: sendComplexMessage
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
private ClientResponse sendComplexMessage(String recipient) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
FormDataMultiPart formData = new FormDataMultiPart();
formData.field("from", "Mailgun User <[email protected]" + MAILGUN_DOMAIN_NAME + ">");
formData.field("to", recipient);
formData.field("subject", "Complex Mailgun Example");
formData.field("html", "<html>HTML <strong>content</strong></html>");
ClassLoader classLoader = getClass().getClassLoader();
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
WebResource webResource = client.resource(
"https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(ClientResponse.class, formData);
}
示例5: sendComplexMessage
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
@SuppressWarnings("VariableDeclarationUsageDistance")
private ClientResponse sendComplexMessage(String recipient) {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
WebResource webResource =
client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
FormDataMultiPart formData = new FormDataMultiPart();
formData.field("from", "Mailgun User <[email protected]" + MAILGUN_DOMAIN_NAME + ">");
formData.field("to", recipient);
formData.field("subject", "Complex Mailgun Example");
formData.field("html", "<html>HTML <strong>content</strong></html>");
ClassLoader classLoader = getClass().getClassLoader();
File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
return webResource
.type(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(ClientResponse.class, formData);
}
示例6: upload
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
public ClientResponse<File> upload(String url, File f, Headers... headers) {
@SuppressWarnings("resource")
FormDataMultiPart form = new FormDataMultiPart();
form.bodyPart(new FileDataBodyPart("file", f, MediaType.APPLICATION_OCTET_STREAM_TYPE));
String urlCreated = createURI(url);
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
WebResource webResource = Client.create(cc).resource(urlCreated);
Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA).accept("text/plain");
for (Headers h : headers) {
builder.header(h.getKey(), h.getValue());
}
com.sun.jersey.api.client.ClientResponse clienteResponse = null;
clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, form);
return new ClientResponse<File>(clienteResponse, File.class);
}
示例7: createFileResponse
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
protected ClientResponse createFileResponse(String method, String path,
File file) throws TogglApiResponseException {
ClientResponse response;
setupCurrentUser();
try {
WebResource webResource = client.resource(path);
FileDataBodyPart fdp = new FileDataBodyPart("file", file,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
formDataMultiPart.bodyPart(fdp);
response = webResource.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON_TYPE)
.header("X-Atlassian-Token", "nocheck")
.method(method, ClientResponse.class, formDataMultiPart);
} catch (Exception e) {
throw new TogglApiResponseException(TogglApiContextFactory
.getContext().getMessage(Messages.ERROR_WRITING_DATA), e);
}
return response;
}
示例8: uploadZippedApplicationTemplate
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
/**
* Uploads a ZIP file and loads its application template.
* @param applicationFile a ZIP archive file
* @throws ManagementWsException if a problem occurred with the applications management
* @throws IOException if the file was not found or is invalid
*/
public void uploadZippedApplicationTemplate( File applicationFile ) throws ManagementWsException, IOException {
if( applicationFile == null
|| ! applicationFile.exists()
|| ! applicationFile.isFile())
throw new IOException( "Expected an existing file as parameter." );
this.logger.finer( "Loading an application from " + applicationFile.getAbsolutePath() + "..." );
FormDataMultiPart part = new FormDataMultiPart();
part.bodyPart( new FileDataBodyPart( "file", applicationFile, MediaType.APPLICATION_OCTET_STREAM_TYPE));
WebResource path = this.resource.path( UrlConstants.APPLICATIONS ).path( "templates" );
ClientResponse response = this.wsClient.createBuilder( path )
.type( MediaType.MULTIPART_FORM_DATA_TYPE )
.post( ClientResponse.class, part );
if( Family.SUCCESSFUL != response.getStatusInfo().getFamily()) {
String value = response.getEntity( String.class );
this.logger.finer( response.getStatusInfo() + ": " + value );
throw new ManagementWsException( response.getStatusInfo().getStatusCode(), value );
}
this.logger.finer( String.valueOf( response.getStatusInfo()));
}
示例9: uploadTestImage
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
static private String uploadTestImage() {
TwitterAdsClient.setTrace(false);
TwitterAdsClient.setSandbox(true);
// upload image
String domain = "https://upload.twitter.com";
String resource = "/1.1/media/upload.json";
String filePath = "src/test/resources/angry.bird.800.320.png";
ClientService clientService = ClientServiceFactory.getInstance();
Client client = clientService.getClient();
WebResource webResource = client.resource(domain);
final FileDataBodyPart filePart = new FileDataBodyPart("media", new File(filePath));
final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multiPart = (FormDataMultiPart) formDataMultiPart.bodyPart(filePart);
ClientResponse response = webResource.path(resource).type(MediaType.MULTIPART_FORM_DATA_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, multiPart);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
JsonObject jsonObj = new JsonParser().parse(output).getAsJsonObject();
return jsonObj.get("media_id_string").getAsString();
}
示例10: persistAttachments
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
private void persistAttachments(ODocument item, Issue issue) throws IOException {
AttachmentStorage storage = new AttachmentStorage();
String id = item.field(FieldNames.ID);
List<Attachment> attachments = storage.readAttachments(Long.parseLong(id));
if (attachments.size() > 0) {
List<String> alreadyExportedAttachments = getAlreadyExportedAttachments(issue);
final FormDataMultiPart multiPart = new FormDataMultiPart();
int newlyAdded = 0;
for (Attachment attachment : attachments) {
// check if already exported
if (!alreadyExportedAttachments.contains(attachment.getPath().getFileName().toString())) {
final File fileToUpload = attachment.getPath().toFile();
if (fileToUpload != null) {
multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE));
newlyAdded++;
}
}
}
if (newlyAdded > 0) {
try {
getRestAccess().postMultiPart(issue.getSelfPath() + "/attachments", multiPart);
} catch (Exception e) {
LOGGER.severe("Could not upload attachments");
}
}
}
}
示例11: postAttachment
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
/**
* Add one or more attachments to an issue.
*
* @param issue Issue object
* @return List
* @throws JsonParseException json parsing failed
* @throws JsonMappingException json mapping failed
* @throws IOException general IO exception
*/
public List<Attachment> postAttachment(Issue issue) throws JsonParseException, JsonMappingException, IOException {
List<File> files = issue.getFields().getFileList();
if (files == null || files.size() == 0) {
throw new IllegalStateException("Oops! Attachment Not Found.");
}
if ((issue.getId() == null || issue.getId().isEmpty())
&& (issue.getKey() == null || issue.getKey().isEmpty())) {
throw new IllegalStateException("Oops! Issue id or Key not set.");
}
String idOrKey = issue.getId() == null ? issue.getKey() : issue.getId();
FormDataMultiPart form = new FormDataMultiPart();
for (int i = 0; i < files.size(); i++) {
// The name of the multipart/form-data parameter that contains attachments must be "file"
FileDataBodyPart fdp = new FileDataBodyPart("file", files.get(i));
form.bodyPart(fdp);
}
client.setResourceName(Constants.JIRA_RESOURCE_ISSUE + "/" + idOrKey + "/attachments");
ClientResponse response = client.postMultiPart(form);
String content = response.getEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, true);
TypeReference<List<Attachment>> ref = new TypeReference<List<Attachment>>() {
};
List<Attachment> res = mapper.readValue(content, ref);
return res;
}
示例12: setParameter
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
@Override
public RunBuilder setParameter(String name, File value)
{
form.field("name", name);
FileDataBodyPart fdp = new FileDataBodyPart(
"file" + fileCounter, value, MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp);
insertComma();
json.append("{'name': '").append(name).append("', 'file': 'file")
.append(fileCounter).append("'}");
return this;
}
示例13: sendPii
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
private String sendPii(File file, String stickyPolicy, Long uid){
try {
Client client = Client.create();
WebResource webResource = client.resource(SERVER_URL);
ClientResponse response;
FormDataMultiPart form = new FormDataMultiPart();
form.bodyPart(new FileDataBodyPart("file", file));
form.field("stickyPolicy", stickyPolicy);
if(uid != null){
form.field("uniqueId", uid.toString());
response = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, form);
}else{
response = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.put(ClientResponse.class, form);
}
if (response.getStatus() != 201) {
System.out.println("Fi-ware return error status: " + response.getStatus());
}
String repStr = response.getEntity(String.class);
System.out.println("Fi-ware stored the pii and its policy with success. PiiId= " + repStr);
return repStr;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例14: sendPii
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
private String sendPii(File file, String stickyPolicy, Long uid){
try {
Client client = Client.create();
WebResource webResource = client.resource(SERVER_URL);
ClientResponse response;
FormDataMultiPart form = new FormDataMultiPart();
form.bodyPart(new FileDataBodyPart("file", file));
form.field("stickyPolicy", stickyPolicy);
if(uid != null){
form.field("uniqueId", uid.toString());
response = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, form);
}else{
response = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON)
.put(ClientResponse.class, form);
}
if (response.getStatus() != 201) {
System.out.println("Fi-ware return error status: " + response.getStatus());
}
String repStr = response.getEntity(String.class);
System.out.println("Fi-ware stored the pii and its policy with success. PiiId= " + repStr);
return repStr;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例15: main
import com.sun.jersey.multipart.file.FileDataBodyPart; //导入依赖的package包/类
public static void main(String args[]) throws Exception {
Properties properties = new Properties();
InputStream input = null;
try {
input = new FileInputStream("src/main/resources/config.properties");
properties.load(input);
} catch (IOException ex) {
ex.printStackTrace();
}
ClientServiceFactory.consumerKey = properties.getProperty("consumer.key");
ClientServiceFactory.consumerSecret = properties.getProperty("consumer.secret");
ClientServiceFactory.accessToken = properties.getProperty("access.token");
ClientServiceFactory.accessTokenSecret = properties.getProperty("access.secret");
String domain = "https://upload.twitter.com";
String resource = "/1.1/media/upload.json";
// String filePath = "C:/temp/twitter.jpeg";
String filePath = "src/test/resources/angry.bird.800.320.png";
ClientService clientService = ClientServiceFactory.getInstance();
Client client = clientService.getClient();
WebResource webResource = client.resource(domain);
webResource.addFilter(new LoggingFilter());
final FileDataBodyPart filePart = new FileDataBodyPart("media", new File(filePath));
final FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multiPart = (FormDataMultiPart) formDataMultiPart.bodyPart(filePart);
ClientResponse response = webResource.path(resource).type(MediaType.MULTIPART_FORM_DATA_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, multiPart);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println(output);
JsonObject jsonObj = new JsonParser().parse(output).getAsJsonObject();
String mediaId = jsonObj.get("media_id_string").getAsString();
System.out.println("media id = "+ mediaId);
}