• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File upload and ^M chars

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I wrote a code using apache' fileUpload and when a file is uploaded from Windows to Unix env, there are ^M chars at the end. This code is deployed on Unix, but a file can be uploaded from Windows. I know how to use some unix commands or dos2unix from windows to remove the ^M chars, but this has to be done programmatically.

A byte array of windows data is written to file output stream to unix env.

Any clue...

thanks
Rams
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^M is the return character - a hallmark of text files created on Windows. I bet the characters are there before the file gets uploaded, it's just that the Window applications don't display it. Do a byte-for-byte comparison of the file before and after the upload using a hex editor or a diff tool - they're in all likelihood identical.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Windows uses \r\n for line breaks - carriage return followed by line feed. Unix only uses \n. The ^M you're seeing is the \r character that isn't recognized by your Unix text editor (don't tell me: VI or Emacs; I've seen them in VI as well).

To remove these you can read the file using a BufferedReader and its readLine() method, then write it to a new file. The readLine() method strips away all line breaks, including \n and \r\n. The writing will use the system default for line breaks, so \n on Unix.

But keep in mind that the new file will not be identical to the original file. If the file is later retrieved and downloaded by a Windows user then the lack of \r may cause problems in Windows text editors. Especially Notepad is notorious for displaying little blocks (like []) when encountering \n without a preceding \r.
 
Ramu Nur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Thanks. I removed all '\r' before uploading, and working fine.

--Rams
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic