服务器代码没问题。因为web-〉server = image文件可以上传,而Android -〉server= imge文件不能上传,所以Android代码无法将img发送到jsp(Tomcat服务器)。
Android服务器:
package com.example.nfc_s;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity; import android.content.Intent; import
android.database.Cursor; import android.graphics.Bitmap; import
android.net.Uri; import android.os.Bundle; import
android.os.StrictMode; import android.provider.MediaStore; import
android.util.Log; import android.view.View; import
android.widget.Button; import android.widget.ImageView; import
android.widget.Toast;
import java.io.DataOutputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity2 extends AppCompatActivity {
ImageView imageView = null;
Button button = null;
private final int REQ_CODE_SELECT_IMAGE = 100;
private String img_path = new String();
private String serverURL = "http://localhost:8080/testDB2/upload.jsp";
//<<서버주소
private Bitmap image_bitmap_copy = null;
private Bitmap image_bitmap = null;
private String imageName = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.permitDiskReads()
.permitDiskWrites()
.permitNetwork().build());
imageView = (ImageView) findViewById(R.id.imageView);
//이미지를 띄울 위젯
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_CODE_SELECT_IMAGE);
}
});
button = (Button) findViewById(R.id.button);
//이미지 전송 버튼
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DoFileUpload(serverURL, img_path);
Toast.makeText(getApplicationContext(), "이미지 전송 성공", Toast.LENGTH_SHORT).show();
Log.d("Send", "Success");
}
});
}//end of onCreate()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getBaseContext(), "resultCode : " + data, Toast.LENGTH_SHORT).show();
if (requestCode == REQ_CODE_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
try {
img_path = getImagePathToUri(data.getData()); //이미지의 URI를 얻어 경로값으로 반환.
Toast.makeText(getBaseContext(), "img_path : " + img_path, Toast.LENGTH_SHORT).show();
//이미지를 비트맵형식으로 반환
image_bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
data.getData());
//사용자 단말기의 width , height 값 반환
int reWidth = (int) (getWindowManager().getDefaultDisplay().getWidth());
int reHeight = (int) (getWindowManager().getDefaultDisplay().getHeight());
//image_bitmap 으로 받아온 이미지의 사이즈를 임의적으로 조절함. width: 400 , height: 300
image_bitmap_copy = Bitmap.createScaledBitmap(image_bitmap, 400, 300, true);
ImageView image = (ImageView) findViewById(R.id.imageView); //이미지를 띄울 위젯 ID값
image.setImageBitmap(image_bitmap_copy);
} catch (Exception e) {
e.printStackTrace();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}//end of onActivityResult()
public String getImagePathToUri(Uri data) {
//사용자가 선택한 이미지의 정보를 받아옴
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(data, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//이미지의 경로 값
String imgPath = cursor.getString(column_index);
Log.d("test", imgPath);
//이미지의 이름 값
String imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1);
Toast.makeText(MainActivity2.this, "이미지 이름 : " + imgName, Toast.LENGTH_SHORT).show();
this.imageName = imgName;
DoFileUpload("http://localhost:8080/testDB2/upload.jsp",imgPath);
return imgPath;
}//end of getImagePathToUri()
public void DoFileUpload(String apiUrl, String absolutePath) {
HttpFileUpload(apiUrl, "", absolutePath);
}
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
public void HttpFileUpload(String urlString, String params, String fileName) {
try {
FileInputStream mFileInputStream = new FileInputStream(fileName);
URL connectUrl = new URL(urlString);
Log.d("Test", "mFileInputStream is " + mFileInputStream);
// HttpURLConnection 통신
HttpURLConnection conn = (HttpURLConnection) connectUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// write data
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = mFileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
Log.d("Test", "image byte is " + bytesRead);
// read image
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = mFileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Test", "File is written");
mFileInputStream.close();
dos.flush();
// finish upload...
// get response
InputStream is = conn.getInputStream();
StringBuffer b = new StringBuffer();
for (int ch = 0; (ch = is.read()) != -1; ) {
b.append((char) ch);
}
is.close();
Log.e("Test", b.toString());
} catch (Exception e) {
Log.d("Test", "exception " + e.getMessage());
// TODO: handle exception
}
} // end of HttpFileUpload() } //end of class
1条答案
按热度按时间pobjuy321#
关键行是:
我怀疑您是否在Android设备 * 上运行Tomcat *,在本例中应该是
localhost
。将其更改为运行tomcat的服务器的实际名称或IP地址。