Even if you don't use Apache Camel (system linkage), it's convenient just to use a convenient library, so try using it.
However, the "text file output" in the example written here is easy without installing a library. .. ..
I think.
I think that you can use various functions by just changing the options, so please try it.
-Output the character string "aaaa" to a file -Output file name "test / a4.txt" ・ If there is no test folder, it will be created automatically. -File is overwritten
import org.apache.camel.*;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.main.Main;
public class FileTest {
public static Main camel = new Main();
public static void main(String[] args) throws Exception {
camel.start();
ProducerTemplate producer = camel.getCamelTemplate();
//Data creation
Exchange exchange = new DefaultExchange(producer.getCamelContext());
Message message = exchange.getIn();
message.setHeader(Exchange.FILE_NAME, "test/a4.txt");
message.setBody("aaaa"); //Output content is text
//File output
String directory = ".";
Exchange result = producer.send("file://" + directory, exchange);
//Error checking
if (result.getException() != null)
throw result.getException();
}
}
pom.xml part
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel-ver}</version>
</dependency>
Rewrite the options below
Exchange result = producer.send("file://" + directory + "?fileExist=Append", exchange);
Rewrite the options below
Exchange result = producer.send("file://" + directory + "?fileExist=Fail", exchange);
Rewrite the following implementation. filename.txt is copied to a4.txt The content is a copy and the file name is test / a4.txt.
message.setHeader(Exchange.FILE_NAME, "test/a4.txt");
message.setBody(new File("filename.txt"));
Rewrite the following implementation. filename.txt is copied to a4.txt
File file = new File("bbb.txt");
message.setBody(file);
message.setHeader(Exchange.FILE_LAST_MODIFIED, file.lastModified());
Exchange result = producer.send("file://" + directory + "?keepLastModified=true", exchange);
Rewrite the following implementation. filename.txt is copied to a4.txt
File file = new File("bbb.txt");
message.setBody(file);
message.setHeader(Exchange.FILE_LAST_MODIFIED, file.lastModified());
Exchange result = producer.send("file://" + directory + "?keepLastModified=true&chmod=777", exchange);
For other options http://camel.apache.org/file2.html
-The result of accessing by HTTPS is entered in the response
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.main.Main;
public class HttpTest {
public static Main camel = new Main();
public static void main(String[] args) throws Exception {
camel.start();
ProducerTemplate producer = camel.getCamelTemplate();
//Data creation
Exchange exchange = new DefaultExchange(producer.getCamelContext());
Message message = exchange.getIn();
//HTTP access
Exchange result = producer.send("https://www.google.co.jp", exchange);
//Error checking
if (result.getException() != null)
throw result.getException();
//Get results
String response = result.getOut().getBody(String.class);
System.out.println(response);
}
}
pom.Part of xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${camel-ver}</version>
</dependency>
I will rewrite it as follows
Exchange result = producer.send("http://192.168.33.10/member/bbb.html?authMethod=Basic&authUsername=secret&authPassword=aaa", exchange);
Recommended Posts