java代码:
package com.ljq.action;
import java.io.File;
import org.apache.struts.action.ActionForm;
public class MyFrom extends ActionForm {
private static final long serialVersionUID = 1L;
// 上传文件域
private File image;
// 上传文件类型
private String imageContentType;
// 封装上传文件名
private String imageFileName;
// 接受依赖注入的属性
private String savePath;
/**
* 文件存放目录
*
* @return
*/
public String getSavePath() throws Exception{
return this.getServlet().getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
}
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 获取Android端上传过来的信息
*
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class UploadAction extends Action {
/* // 上传文件域
private File image;
// 上传文件类型
private String imageContentType;
// 封装上传文件名
private String imageFileName;
// 接受依赖注入的属性
private String savePath;*/
private MyFrom myfrom;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MyFrom myfrom=(MyFrom) form;
FileOutputStream fos = null;
FileInputStream fis = null;
try {
System.out.println("获取Android端传过来的普通信息:");
System.out.println("用户名:"+request.getParameter("username"));
System.out.println("密码:"+request.getParameter("pwd"));
System.out.println("年龄:"+request.getParameter("age"));
System.out.println("文件名:"+request.getParameter("fileName"));
System.out.println("获取Android端传过来的文件信息:");
System.out.println("文件存放目录: "+myfrom.getSavePath());
System.out.println("文件名称: "+myfrom.getImageFileName());
// System.out.println("文件大小: "+myform.length());
System.out.println("文件类型: "+myfrom.getImageContentType());
fos = new FileOutputStream(myfrom.getSavePath() + "/" + myfrom.getImageFileName());
fis = new FileInputStream(myfrom.getImage());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
System.out.println("文件上传成功");
} catch (Exception e) {
System.out.println("文件上传失败");
e.printStackTrace();
} finally {
close(fos, fis);
}
return mapping.findForward("success");
}
/* @Override
public String execute() {
HttpServletRequest request = ServletActionContext.getRequest();
}
*/
public MyFrom getMyfrom() {
return myfrom;
}
public void setMyfrom(MyFrom myfrom) {
this.myfrom = myfrom;
}
private void close(FileOutputStream fos, FileInputStream fis) {
if (fis != null) {
try {
fis.close();
fis=null;
} catch (IOException e) {
System.out.println("FileInputStream关闭失败");
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
fis=null;
} catch (IOException e) {
System.out.println("FileOutputStream关闭失败");
e.printStackTrace();
}
}
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="myForm" type="com.ljq.action.MyFrom">
</form-bean>
</form-beans>
<action-mappings>
<action path="/upload" type="com.ljq.action.UploadAction" input="/error.jsp" name="myForm" scope="request" parameter="image">
<forward name="success" path="/success.jsp"></forward>
</action>
</action-mappings>
</struts-config>
望大侠帮看看
4 个解决方案
#1
安卓咋上传的?http client?
#2
package com.example.androidupload;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import utils.FormFile;
import utils.SocketHttpRequester;
public class MainActivity extends Activity {
private File file;
private Handler handler;
private static final String TAG="TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
file = new File(Environment.getExternalStorageDirectory(), "123.jpg");
Log.i(TAG, "照片文件是否存在:"+file);
// handler=new Handler();
// handler.post(runnable);
new Thread(runnable).start();
}
Runnable runnable = new Runnable() {
public void run() {
Log.i(TAG, "runnable run");
uploadFile(file);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// handler.postDelayed(runnable, 5000);
}
};
/**
* 上传图片到服务器
*
* @param imageFile 包含路径
*/
public void uploadFile(File imageFile) {
Log.i(TAG, "upload start");
try {
// String requestUrl = "http://192.168.1.101:8083/upload/upload/execute.do";
String requestUrl = "http://192.168.0.242:8080/upload/upload/execute.do";
// String requestUrl = "http://localhost:8080/upload/upload/execute.do";
//请求普通信息
Map<String, String> params = new HashMap<String, String>();
params.put("username", "张三");
params.put("pwd", "zhangsan");
params.put("age", "21");
params.put("fileName", imageFile.getName());
//上传文件
FormFile formfile = new FormFile(imageFile.getName(), imageFile, "image", "application/octet-stream");
SocketHttpRequester.post(requestUrl, params, formfile);
Log.i(TAG, "upload success");
} catch (Exception e) {
Log.i(TAG, "upload error");
e.printStackTrace();
}
Log.i(TAG, "upload end");
}
}
这个是安卓端的代码
#3
直接用流,安卓跟Javaweb一样的
#4
楼主解决了吗?我也遇到这个问题,解决了能告诉我一下吗?
#1
安卓咋上传的?http client?
#2
package com.example.androidupload;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import utils.FormFile;
import utils.SocketHttpRequester;
public class MainActivity extends Activity {
private File file;
private Handler handler;
private static final String TAG="TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
file = new File(Environment.getExternalStorageDirectory(), "123.jpg");
Log.i(TAG, "照片文件是否存在:"+file);
// handler=new Handler();
// handler.post(runnable);
new Thread(runnable).start();
}
Runnable runnable = new Runnable() {
public void run() {
Log.i(TAG, "runnable run");
uploadFile(file);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// handler.postDelayed(runnable, 5000);
}
};
/**
* 上传图片到服务器
*
* @param imageFile 包含路径
*/
public void uploadFile(File imageFile) {
Log.i(TAG, "upload start");
try {
// String requestUrl = "http://192.168.1.101:8083/upload/upload/execute.do";
String requestUrl = "http://192.168.0.242:8080/upload/upload/execute.do";
// String requestUrl = "http://localhost:8080/upload/upload/execute.do";
//请求普通信息
Map<String, String> params = new HashMap<String, String>();
params.put("username", "张三");
params.put("pwd", "zhangsan");
params.put("age", "21");
params.put("fileName", imageFile.getName());
//上传文件
FormFile formfile = new FormFile(imageFile.getName(), imageFile, "image", "application/octet-stream");
SocketHttpRequester.post(requestUrl, params, formfile);
Log.i(TAG, "upload success");
} catch (Exception e) {
Log.i(TAG, "upload error");
e.printStackTrace();
}
Log.i(TAG, "upload end");
}
}
这个是安卓端的代码
#3
直接用流,安卓跟Javaweb一样的
#4
楼主解决了吗?我也遇到这个问题,解决了能告诉我一下吗?