这是客户端代码,在客户端,服务器响应代码GIVES ERROR 500.on服务器端console.log方法显示未定义
public class HttpUpload extends AsyncTask<Void, Integer, Integer> {
private Context context;
private String imgPath;
int serverResponseCode = 0;
public HttpUpload(Context context, String imgPath) {
super();
this.context = context;
this.imgPath = imgPath;
}
@Override
protected Integer doInBackground(Void... params) {
String upLoadServerUri = "http://192.168.10.182:8080/uploadimage";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(imgPath);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File Does not exist");
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
Log.i("uploadFile", "" + fileInputStream);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file",imgPath );
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ imgPath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
return null;
}
}
这是req.files显示为undefined
的服务器代码 var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
var port = process.env.PORT || 8080;
var config = require("./config");
var requests = require("./model")(config);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/uploadimage',function(req,res){
console.log("recieved files"+req.files);
});
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.listen(port);
console.log('The App runs on port ' + port);
答案 0 :(得分:0)
你错过了一个能够解析multipart/form-data
个请求的中间件。 body-parser
模块当前未提供一个模块。您可以为multipart/form-data
查看multer
之类的内容。
答案 1 :(得分:0)
您可以使用multer来实现这一目标。
定义中间件如下
var filename;
var multer1 = multer({ dest: './public/uploads/images/',
onFileUploadComplete: function (file) {
filename = file.name;
}
});
dest是您要存储文件的目标位置。上传完成后,您可以将文件名作为file.name。 在路由器中使用上面的中间件。
app.post('/uploadimage', multer1, function(req, res){
res.end(filename);
});
不要忘记在文件开头导入multer
var multer = require('multer');