共计 2692 个字符,预计需要花费 7 分钟才能阅读完成。
最近时间在做的项目中需要用到 Android 前端上传服务器, 第一次做文件上传的服务器端, 查了各种资料, 踩了各种坑, 记录一下最终的实现方式
** 一. 服务器端 **
服务器端使用的是 ssh 框架, 文件实现逻辑:
1. 获取用户上传的文件
2. 将文件保存到文件夹
3. 保存成功后将文件的索引存入数据库,
1. 获取用户上传的文件
完整 action 如下:
public class UploadAction extends ActionSupport implements ModelDriven<QualityBean>, ServletResponseAware, ServletRequestAware {
private HttpServletResponse response;
private HttpServletRequest request;
//********* 重点 ******
// 多文件上传的文件 key, 相当于获取 request.getParameter(key)
// 如果传入的 key 不对, 上传的文件为空, 报空指针异常,
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
private List<String> dataUrl;
public void setDataUrl(List<String> dataUrl) {this.dataUrl = dataUrl;}
public List<File> getFile() {return file;}
public void setFile(List<File> file) {this.file = file;}
public List<String> getFileFileName() {return fileFileName;}
public void setFileFileName(List<String> fileFileName) {this.fileFileName = fileFileName;}
public List<String> getFileContentType() {return fileContentType;}
public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}
private QualityBean bean = new QualityBean();
public void setBean(QualityBean bean) {this.bean = bean;}
public String upload() throws Exception {dataUrl = new ArrayList<String>();
String imgpath = "D:/loadtest";
System.out.println("file-size" + file.size());
if (file != null) {for (int i = 0; i < file.size(); ++i) {InputStream is = new FileInputStream(file.get(i));
dataUrl.add(imgpath + this.getFileFileName().get(i));
File destFile = new File(imgpath, this.getFileFileName().get(i));
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {os.write(buffer, 0, length);
}
is.close();
os.close();
// 往数据库添加 -- 略
// 返回数据, 真实项目中返回自己逻辑判断
BooleanBean bean = new BooleanBean();
bean.setUpdate(true);
DataUtils.writeBeanDate(response, bean);
}
}
return null;
}
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {this.request = httpServletRequest;}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {this.response = httpServletResponse;}
@Override
public QualityBean getModel() {return bean;}
}
** 二.Android 端代码实现 **
final List<String> list = new ArrayList<>();
list.add("/storage/emulated/0/Boohee/1565153408424.jpg");
list.add("/storage/emulated/0/Boohee/1564797245581.jpg");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {Map<String, RequestBody> params = new HashMap<>();
RequestBody pictureNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), "file");
for (String str : list) {File picture = new File(str);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), picture);
params.put("file\"; filename=\""+ picture.getName() +"", requestFile);
}
mvpPresenter.submit("韶关 1 店",pictureNameBody, params);
}
});
上面 file 作为文件上传的 key, 两端必须保持一致
正文完