08-05-2008 03:31 AM
String adresse= "/alfresco/wcservice/sample/upload?";
try {
//encodage des paramètres de la requête
String donnees = URLEncoder.encode("filename", "UTF-8")+
"="+URLEncoder.encode(nomDoc, "UTF-8");
donnees += "&"+URLEncoder.encode("content", "UTF-8")+
"=" + URLEncoder.encode(content, "UTF-8");
donnees += "&"+URLEncoder.encode("mimetype", "UTF-8")+
"=" + URLEncoder.encode(mimetype, "UTF-8");
donnees += "&"+URLEncoder.encode("title", "UTF-8")+
"=" + URLEncoder.encode(title, "UTF-8");
donnees += "&"+URLEncoder.encode("description", "UTF-8")+
"=" + URLEncoder.encode(description, "UTF-8");
donnees += "&"+URLEncoder.encode("TypeExam", "UTF-8")+
"=" + URLEncoder.encode(TypeExam, "UTF-8");
donnees += "&"+URLEncoder.encode("nodeid", "UTF-8")+
"=" + URLEncoder.encode(nodeid, "UTF-8");
donnees += "&"+URLEncoder.encode("ticket", "UTF-8")+
"=" + URLEncoder.encode(getTicket(), "UTF-8");
//création de la connection
URL url = new URL(getUrlContext()+adresse);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
//envoi de la requête
writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(donnees);
writer.flush();
//lecture de la réponse
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((ligne = reader.readLine()) != null) {
System.out.println(ligne);
}
var filename = null;
var content = null;
var title = "";
var description = "";
var nodeid = "";
var TypeExam = "";
var mimetype = "";
// locate file attributes
filename = args['filename'];
content = args['content'];
mimetype = args['mimetype'];
title = args['title'];
description = args['description'];
nodeid = args ['nodeid'];
TypeExam = args['TypeExam'];
// ensure mandatory file attributes have been located
if (filename == undefined || content == undefined)
{
status.code = 400;
status.message = "Uploaded file cannot be located in request";
status.redirect = true;
}
else
{
// get folder to upload into with nodeid
if ((nodeid) && (nodeid != ""))
{
model.folder = search.findNode("workspace://SpacesStore/" +nodeid);
}
upload = model.folder.createFile(filename) ;
upload.specializeType("{my.new.model}compte_rendu");
upload.properties.content.write(content);
upload.properties.content.encoding = "UTF-8";
upload.properties.content.mimetype = mimetype;
upload.properties.title = title;
upload.properties.description = description;
upload.properties.TypeExam = TypeExam;
upload.save();
// setup model for response template
model.upload = upload;
}
Quand j'utilise upload.properties.content.write(content), j'ouvre le document il contient ceci "Le contenu de l'élément est manquant : 08-06-2008 10:04 AM
upload.properties.content.content = content;
Avec la version 2.1.1 d'Alfresco ça fonctionne mais j'avais des problème avec l'instruction :upload.properties.content.encoding = "UTF-8";
je l'ai donc enlevée et ça passe.08-07-2008 03:44 AM
08-07-2008 10:57 AM
upload.properties.content.write(content);
upload.properties.content.mimetype = "UTF-8";
upload.properties.title = "mon fichier uploadé";
upload.save();
08-08-2008 03:53 AM
08-08-2008 12:00 PM
08-20-2008 10:56 AM
public String uploadDocs(String title, String description, String correspondant,String nodeid){
HttpURLConnection conn = null;
BufferedReader br = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
InputStream is = null;
OutputStream os = null;
boolean ret = false;
String StrMessage = "";
String exsistingFileName = "D:\\Docs_scannes\\PDF\\advanced-workflow-article.pdf";
String lineEnd = "\r\n";
String twoHyphens = "–";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String responseFromServer = "";
String urlString = "http://localhost:8082/alfresco/wcservice/sample/uploadNew";
try
{
//—————— CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream( new
File(exsistingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"nodeid\"" + lineEnd + lineEnd);
System.out.println(nodeid + lineEnd);
dos.writeBytes( nodeid + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";"
+ " filename=\"" + exsistingFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"title\"" + lineEnd + lineEnd);
dos.writeBytes( title + lineEnd);
System.out.println(title + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"desc\"" + lineEnd + lineEnd);
dos.writeBytes( description + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"author\"" + lineEnd + lineEnd);
dos.writeBytes( correspondant + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"ticket\"" + lineEnd + lineEnd);
dos.writeBytes( getTicket() + lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form…
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data…
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
System.out.println("From ServletCom CLIENT REQUEST:"+ex);
}
catch (IOException ioe)
{
System.out.println("From ServletCom CLIENT REQUEST:"+ioe);
}
//—————— read the SERVER RESPONSE
try
{
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readUTF()) != null)
{
System.out.println("Server response is: "+str);
System.out.println("");
}
inStream.close();
}
catch (IOException ioex)
{
System.out.println("From (ServerResponse): "+ioex);
}
return urlString;
}
Et le script correspondant:
// get folder to upload into with nodeid
if ((args.nodeid) && (args.nodeid != ""))
{
model.folder = search.findNode("workspace://SpacesStore/" + args.nodeid);
}
var filename = null;
var content = null;
var mimetype = null;
encoding = null;
var title = null;
var description = null;
var author = null;
var name = null;
// locate file attributes
for each (field in formdata.fields)
{
if (field.name == "title")
{
title = field.value;
}
else if (field.name == "desc")
{
description = field.value;
}
else if (field.name == "author")
{
author = field.value;
}
else if (field.name == "name")
{
// for having name properties different than original filename
name = field.value;
}
else if (field.name == "file" && field.isFile)
{
filename = field.filename;
content = field.content;
encoding = "UTF-8";
}
}
if ((args.mimetype) && (args.mimetype != ""))
{
mimetype = args.mimetype;
}
// ensure folder and mandatory file attributes have been located
if (model.folder == null || filename == undefined || content == undefined)
{
status.code = 400;
status.message = "Uploaded file cannot be located in request";
status.redirect = true;
}
else
{
// create document in model.folder for uploaded file
upload = model.folder.createFile("upload" + model.folder.children.length + "_" + filename);
upload.properties.content.write(content);
upload.properties.content.encoding = encoding;
upload.properties.content.mimetype = mimetype;
if (name != null)
{
upload.properties.name = name;
}
else
{
upload.properties.name = filename;
}
if (title != null)
{
upload.properties.title = title;
}
if (description != null)
{
upload.properties.description = description;
}
if (author != null)
{
upload.properties.author = author;
}
upload.save();
// setup model for response template
model.upload = upload;
}
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.