Skip to content

Commit

Permalink
Update sample, upgrade the targetSdkVersion to 30.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanzhenjie committed Jan 29, 2023
1 parent 510af2e commit f6f37c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
6 changes: 3 additions & 3 deletions config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ ext {

androidBuild = [
applicationId : 'com.yanzhenjie.andserver.sample',
compileSdkVersion : 29,
compileSdkVersion : 30,

libraryMinSdkVersion : 9,
libraryTargetSdkVersion: 29,
libraryTargetSdkVersion: 30,
sampleMinSdkVersion : 14,
sampleTargetSdkVersion : 22
sampleTargetSdkVersion : 30
]

deps = [
Expand Down
14 changes: 3 additions & 11 deletions sample/src/main/java/com/yanzhenjie/andserver/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package com.yanzhenjie.andserver.sample;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import androidx.annotation.NonNull;

import com.yanzhenjie.andserver.sample.util.FileUtils;
import com.yanzhenjie.andserver.util.IOUtils;

import java.io.File;
Expand All @@ -41,7 +38,7 @@ public void onCreate() {

if (mInstance == null) {
mInstance = this;
initRootPath(this);
initRootPath();
}
}

Expand All @@ -55,17 +52,12 @@ public File getRootDir() {
return mRootDir;
}

private void initRootPath(Context context) {
private void initRootPath() {
if (mRootDir != null) {
return;
}

if (FileUtils.storageAvailable()) {
mRootDir = Environment.getExternalStorageDirectory();
} else {
mRootDir = context.getFilesDir();
}
mRootDir = new File(mRootDir, "AndServer");
mRootDir = new File(getFilesDir(), "AndServer");
IOUtils.createFolder(mRootDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.yanzhenjie.andserver.sample.model.UserInfo;
import com.yanzhenjie.andserver.sample.util.FileUtils;
import com.yanzhenjie.andserver.sample.util.Logger;
import com.yanzhenjie.andserver.util.Executors;
import com.yanzhenjie.andserver.util.MediaType;

import java.io.File;
Expand Down Expand Up @@ -110,9 +111,23 @@ UserInfo userInfo(@CookieValue("account") String account) {
}

@PostMapping(path = "/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
String upload(@RequestParam(name = "avatar") MultipartFile file) throws IOException {
File localFile = FileUtils.createRandomFile(file);
file.transferTo(localFile);
String upload(@RequestParam(name = "avatar") final MultipartFile file) {
final File localFile = FileUtils.createRandomFile(file);

// We use a sub-thread to process files so that the api '/upload' can respond faster
Executors.getInstance().submit(new Runnable() {
@Override
public void run() {
try {
file.transferTo(localFile);

// Do something ...
} catch (IOException e) {
e.printStackTrace();
}
}
});

return localFile.getAbsolutePath();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,4 @@ public static File createRandomFile(MultipartFile file) {
String uuid = UUID.randomUUID().toString();
return new File(App.getInstance().getRootDir(), uuid + "." + extension);
}

/**
* SD is available.
*
* @return true, otherwise is false.
*/
public static boolean storageAvailable() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File sd = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
return sd.canWrite();
} else {
return false;
}
}
}

0 comments on commit f6f37c9

Please sign in to comment.