I'm developing an app that will send a file using HTTP server (with nanoHTTPD) to another device by typing the sender's IP:port. The transferring is working correctly, but I'm not able to receive the right file name of the sent file (its being named as 'default', without any extension, by the receiver's browser). Here's my code for the HTTP server:
private class WebServer extends NanoHTTPD {
public WebServer()
{
super(8080);
}
#Override
public Response serve(String uri, Method method,
Map header, Map parameters,
Map files) {
//receive my file's path from an intent
Intent intent = getIntent();
String filename = intent.getStringExtra(MainActivity.FILENAME);
FileInputStream file = null;
try {
file = new FileInputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return new NanoHTTPD.Response(Status.OK, "/", file);
}
I thought I could fix it using FileInputStream(new File(String path, String name)) but it still doesn't work right and it still give me a 'default' file name with 0 byte file size.
Can anyone give me some idea how can I get the right file name from the HTTP server? Hope someone can help me here. Thanks!
It depends on what your server (script) accepts, when sending you should add headers with name of your file, this can be:
Content-Disposition: attachment; filename="fname.ext"
example from http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html "19.5.1 Content-Disposition"
Related
I'm hosting my application in azure (Web service + file storage service).
I wish to use Fresco (SimpleDraweeView) in my application, the problem is that i can not give a direct url to the user, as the storage in azure is private.
The only thing i can do is to get the image as byte array (using my web service) and foward this byte array back to the android client.
How can one use simpledraweeview with a byte array instead of a direct link?
I have tried to set an endpoint in my webservice where the user is giving me the image id and the endpoint returns back the byte array, i have tried to use this endpoint as the url for the simpledraweeview.setImageUrl method but with no luck.
According to your description, per my experience, it sounds like you need to create a web app as proxy service to access and response the image hosted on Azure File Storgae back to andoird.
Assumption that you are a Java Web developer, I think the simple way is that you can create a Java web app in Azure App Service, then create a Java servlet for the Java web app and refer to the article How to use File Storage from Java to download the image to pipe the stream to http servlet response, please see the sample code below.
private static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=your_storage_account_name;" +
"AccountKey=your_storage_account_key";
private CloudFileClient fileClient;
/**
* #see HttpServlet#HttpServlet()
*/
public PipeStream4FileStorage() {
super();
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
fileClient = storageAccount.createCloudFileClient();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dirName = request.getParameter("dir");
String fileName = request.getParameter("file");
OutputStream outStream = response.getOutputStream();
try {
// Get a reference to the file share
CloudFileShare share = fileClient.getShareReference("sampleshare");
//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();
//Get a reference to the directory that contains the file
CloudFileDirectory sampleDir = rootDir.getDirectoryReference(dirName);
//Get a reference to the file you want to download
CloudFile file = sampleDir.getFileReference(fileName);
//Write the stream of the file to the httpServletResponse.
file.download(outStream);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
}
}
Note, please see the javadoc for the key function download(OutputStream outStream).
Then, you just need to set the image url for SimpleDraweeView like below.
Uri uri = Uri.parse("https://<your-webapp-name>.azurewebsites.net/<servlet-url-mapping-name>?dir=<dir-name>&file=<file-name>");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri);
I need to read a text stream by using StreamReader from file on android platform. File is about 100k lines, so even editor is getting stuck if i try to load it all to TextAsset or if i use WWW.
I simply need to read that file line by line without loading it all to a string. Then i'll do a tree generation from the lines that i got from the file. (But probably that part doesn't matter, i just need help on file reading part.)
I'm giving the code that i wrote down below. It works perfectly on editor, but fails on android.
I would be glad if anyone tell me, what am i missing.
(ps. english is not my native and this is my first question on the site. so sorry for the any mistakes that i may have done.)
private bool Load(string fileName)
{
try
{
string line;
string path = Application.streamingAssetsPath +"/";
StreamReader theReader = new StreamReader(path + fileName +".txt", Encoding.UTF8);
using (theReader)
{
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
exception = e.Message;
return false;
}
}
You can't use Application.streamingAssetsPath as a path on Android because streaming assets are stored within the JAR file with the application.
From http://docs.unity3d.com/Manual/StreamingAssets.html:
Note that on Android, the files are contained within a compressed .jar
file (which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file then you will need to use additional software to see
inside the .jar archive and obtain the file.
Use WWW like this in a coroutine:
WWW data = new WWW(Application.streamingAssetsPath + "/" + fileName);
yield return data;
if(string.IsNullOrEmpty(data.error))
{
content = data.text;
}
Or, if you really want to keep it simple (and your file is only a few 100k, stick it in a resource folder:
TextAsset txt = (TextAsset)Resources.Load(fileName, typeof(TextAsset));
string content = txt.text;
I used the following code from the answer to this question by #scott How do I upload an image to a ServiceStack service?
[Route("/upload","POST")]
public class UploadFileRequest
{
// Example of other properties you can send with the request
public string[] Tags { get; set; }
}
class MyFileService : Service
{
public bool Post(UploadFileRequest request)
{
// Check a file has been attached
if(Request.Files == null || Request.Files.Length == 0)
throw new HttpError(400, "Bad Request", "No file has been uploaded");
// Save the file
Request.Files[0].SaveTo(Request.Files[0].FileName);
// Maybe store the tags (or any other data in the request)
// request.Tags
return true;
}
}
Then with the JsonServiceClient in your Android app, then your simply need to do this:
var filename = "cab.jpg"; // The path of the file to upload
var client = new JsonServiceClient("http://212.175.132.168/service/api/");
using(var fileStream = File.OpenRead(filename))
{
client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }});
}
I used this with my DTO and in my Android app, but when I try to send it always fails with the following server error:
{"ResponseStatus": {"ErrorCode":"UnauthorizedAccessException","Message":"'C:\\Windows\\SysWOW64\\inetsrv\\a.png' path denied.", 'C:\Windows\SysWOW64\inetsrv\a.png' path denied.
Can anyone share Monodroid ServiceStack Image upload sample?
Thanks.
There is nothing wrong with the example code, that you have taken from my answer given here, which you used in the Monodroid client. It works on Monodroid using the ServiceStack PCL library without modification.
Monodroid:
No modification required.
var filename = "cab.jpg"; // The path of the file to upload
var client = new JsonServiceClient("http://212.175.132.168/service/api/");
using(var fileStream = File.OpenRead(filename))
{
client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }});
}
Server File Permissions Error:
The error message you get when you upload to the ServiceStack service shows that your server process does not have permission to write the file to this directory C:\Windows\SysWOW64\inetsrv.
{
"ResponseStatus":
{
"ErrorCode":"UnauthorizedAccessException",
"Message":"'C:\Windows\SysWOW64\inetsrv\a.png' path denied."
}
}
You need to update the server side service to write the file to a path which the service has permission to.
class MyFileService : Service
{
public bool Post(UploadFileRequest request)
{
// Check a file has been attached
if(Request.Files == null || Request.Files.Length == 0)
throw new HttpError(400, "Bad Request", "No file has been uploaded");
// Replace with a path you have permission to write to
var path = #"c:\temp\image.png";
// Save the file
Request.Files[0].SaveTo(path);
// Maybe store the tags (or any other data in the request)
// request.Tags
return true;
}
}
If you fix the permission error you will see it works.
When another application is sending a file to my app, I get a Uri via the intent.getExtras().get(EXTRA_STREAM) property. I can then get the bytes of the file using an inputstream : new BufferedInputStream(activity.getContentResolver().openInputStream(uri));
Everything's OK and working so far. Now I'd like to show some kind of progress to my user, but I'm not sure of how to get the total number of bytes of the file without reading the stream completely beforehand (which would defeat the whole purpose of the progress bar) ...
I tried ParcelFileDescriptor fileDesc = activity.getContentResolver().openFileDescriptor(uri, "r"); but this only works with uris of type file://....
For example If I receive a file from Skydrive I get a content://.. Uri, as in : content://com.microsoft.skydrive.content.external/external_property/10C32CC94ECB90C4!155/Sunset.480p.mp4
On such Uri I get (unsurprisingly) a "FileNotFoundException : Not a whole file" exception.
Any sure fire way to get the total size of the stream of data I will get ?
Even though InputStream.available() is (almost) never a recommended way of getting file size, it might be a viable solution in your case.
The content is already available locally. A server is not involved. So, the following should return the exact file size:
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
Log.i("TEST", "File Size: " + inputStream.available());
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
I tested this with SkyDrive and Dropbox. The file sizes returned were correct.
There is no general solution for getting the size of a stream, other than reading the entire stream. This is easily proven: One could create a web server that, for some URL, generates a random stream of text that is terminated at a random time. (In fact, I'm sure such URLs exist, whether by design or not :-) In such a case, the size of the stream isn't known until the last byte has been generated, never mind received.
So, the stream size, if it is sent by the server at all, has to be sent in an application-specific manner.
I've never worked with SkyDrive, but a google search for its API turned up this link, which has the following example for Android Java apps:
public void readFile() {
String fileId = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!141";
client.getAsync(fileId, new LiveOperationListener() {
public void onError(LiveOperationException exception, LiveOperation operation) {
resultTextView.setText("Error reading file: " + exception.getMessage());
}
public void onComplete(LiveOperation operation) {
JSONObject result = operation.getResult();
String text = "File info:" +
"\nID = " + result.optString("id") +
"\nName = " + result.optString("name");
resultTextView.setText(text);
}
});
}
Based on other examples on that page, I would guess that something like result.optString("size") (or maybe result.optInt("size") ?) would give you the size of the file.
I have an application that uses a webview in order to display content and the Javascript calls are the controller of my application.
In order to provide a level of security I obfuscated the code. This is not enough as I would like to encrypt the html and js files and then decrypt them at runtime. I packed the apk file with these resources encrypted with RC4 algorithm. When loading the files, I am decrypting the javascript files, load them and then decrypt the html file and load it. However this doesn't work as the webcontent displays a message in the form of: the web page at data:text/html might be temporarily down or it may have moved permanently, etc, etc.
I overloaded onLoadResource in order to see what content is loaded and I can see it loads the Javascript content, but the content loaded is html escaped also.
My questions are:
1. How to secure the html and javascript files (located in assets folder) in order to not be accessible?
2. In case my approach is correct, has anyone any idea on what I am doing wrong?
Thanks!
Below is the code that decrypts and loads the resources:
protected void loadWebContent() {
checkEncryptionEnabled();
loadJSFiles();
logger.info("Loaded js ... going for html");
loadAssetFile("www/index.html", "text/html");
}
private void loadJSFiles() {
String[] jsFilesArray = { "app.js", "iscroll.js", "iui.js", "json.js" };
for (String js : jsFilesArray) {
loadAssetFile("www/js/" + js, "application/javascript");
}
}
private void loadAssetFile(String filePath, String mimeType) {
AssetManager assetMgr = getAssets();
InputStream is = null;
try {
is = assetMgr.open(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] temp = new byte[512];
int bytesRead = -1;
while ((bytesRead = is.read(temp)) > 0) {
baos.write(temp, 0, bytesRead);
}
byte[] encrypted = baos.toByteArray();
String content = null;
/**
* true
* */
if (Config.ENCRYPTION_ENABLED) {
byte[] decrypted = new RC4Encrypter("rc4_key").rc4(encrypted);
content = new String(decrypted, "utf-8");
} else {
content = new String(encrypted, "utf-8");
}
/**
* The webview to use
* */
if("application/javascript".equals(mimeType)) {
webContent.loadUrl("javascript:" + content);
} else {
webContent.loadData(content, mimeType, "utf-8");
}
} catch (IOException ex) {
logger.error(null, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
found the answer for the second question question instead of: webContent.loadData(content, mimeType, "utf-8"); I used: webContent.loadDataWithBaseURL("file:///android_asset/www/", content, mimeType, "utf-8", null); Content is shown with no problems ... However, the first question kind of stands and not; but considering there was no answer for more than a year, I'll consider encrypting data is OK.
Data encryption is OK as long as you can also keep the decryption key confidential, which is not the case in the above code. The hardcoded decryption key can be easily spotted after decompiling the DEX files embedded inside the APK.
If you want to hide the application logic inside the HTML and JavaScript files and if that application logic doesn't require offline capabilities then you could outsource the code of that application logic on a server.
From here you have two choices:
Load the application code dynamically from the server whenever
you need it (and run the application code on the client).
Implement the application logic on the server side, e.g., as a
web service (and run the application code on the server, the client
knows only how to call the web service)
The short answer to your first question is that there is no methodology or technology to perfectly protect your application. I recommend to you to take a look at How to avoid reverse engineering of an APK file? for an overview of possible protection methods.