解决问题:路径中的非法字符

时间:2013-11-07 14:12:25

标签: java xml filepath

我正在开发一个应用程序,它将XML文件和附件发送到以下路径。此路径适用于传真设备。

我不断收到此错误消息:

  

问题处理丢弃文件“\ co1-aux01​​prd01.tampa.healthe \ Fax_Drop \ Outbox \ FaxDropSample1.xml”:路径中的字符非法。

正在创建XML文件和附件,但未进行处理。

public class TestSender {

    public static void main(String[] args) {

        String outBox = "\\\\faxaux\\Fax_Drop\\Outbox";
        String filename = "FaxDrop" + ".xml";
        String filepath = outBox + "\\" + filename;
        Writer writer = null;

        try {
            BufferedImage image;
            URL url = new URL("http://colsolgrp.com/phone/jpg/fax8.jpg");
            image = ImageIO.read(url);
            //File newImage = new File("\\\\faxaux\\Fax_Drop\\Outbox\\AttachmentFolder\\attachment.jpg");
            File newImage = new File("\\\\faxaux\\Fax_Drop\\Outbox\\FaxDrop\\FaxDropImage.jpg");
            newImage.mkdirs();
            newImage.createNewFile();
            ImageIO.write(image, "jpg",newImage);
            System.out.println("File has been written");
        }
        catch(Exception e) {
            System.out.println("Could not create file");
        }

        try {
            File f = new File(filepath);
            f.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(f);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
            writer = new BufferedWriter(outputStreamWriter);
            // Create XML file here
        }
        catch (Throwable ex) {
            ex.printStackTrace();
        }
        finally {
            try {
                writer.close();
            }
            catch (Exception ex) {
                // Do nothing.
            }
        }
        System.out.println("Success");

1 个答案:

答案 0 :(得分:0)

试试这个:

URI u = new URI(URLEncoder.encode("\\co1-aux01prd01.tampa.healthe\Fax_Drop\Outbox\FaxDropSample1.xml"));