public class CoachuploadAction extends ActionSupport implements ServletResponseAware {
private HttpServletResponse response;
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
this.response = httpServletResponse;
}
private File file;//跟form表单中的名字相对应
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String coachupload() throws IOException {
System.out.println("文件上传---");
String fileName = file.getName();
String webPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");
String[] strArr = webPath.split("\\/");
System.out.println(strArr.length); //这里输出3
String str2 = "";
for (int i = 0; i < strArr.length; ++i){
if (i == 0) {
str2 = strArr[i];
System.out.println(i+"i:"+str2);//这里输出a b c
}
if (i >= 1) {
str2 = str2+"/"+strArr[i] ;
System.out.println(i+"i:"+str2);//这里输出a b c
}
if (i==4){
break;
}
}
String savaPath = str2 + "/boxlogin/nuboxsj/idcard";
String realpath = savaPath.replace("%20"," ");
System.out.println("存储地址为:"+savaPath);
//文件保存地址和原文件名建立上传文件输出流
if (fileName != null) {
File file = new File(realpath);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(realpath + "\\" + fileName);
FileInputStream fis = new FileInputStream(getFile());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
response.setContentType("text/json;charset=UTF-8");
ServletActionContext.getResponse().getWriter().print("上传成功");
} else {
response.setContentType("text/json;charset=UTF-8");
ServletActionContext.getResponse().getWriter().print("没接收到文件");
}
return null;
}
}
java 微信小程序文件上传ssh
最新推荐文章于 2020-06-22 11:05:34 发布