上传 移动端
public static void upload(String filePath, final SuperWebView superWebView) {
String url=""; //服务端负责接收数据的链接
RequestParams params = new RequestParams();
params.addBodyParameter("file", new File(filePath));
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpRequest.HttpMethod.POST,
url,
params,
new RequestCallBack<String>() {
public void onStart() {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Toast.makeText(activity, "上传成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(activity, "上传失败", Toast.LENGTH_SHORT).show();
ToolUtil.showShortTip(ToolUtil.activity, "上传失败!");
}
});
}
上传 服务端
public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try{
String nowpath; //当前tomcat的bin目录的路径 如 D:/java/software/apache-tomcat-6.0.14/bin
String tempdir;
nowpath = System.getProperty("user.dir");
tempdir = nowpath+"/temp";
// 解析 request,判断是否有上传文件
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// 创建磁盘工厂,利用构造器实现内存数据储存量和临时储存路径
File file = new File(tempdir);
if(!file.exists() && !file.isDirectory()){
file.mkdir();
}
DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 4, new File(tempdir));
// 产生一新的文件上传处理程式
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置路径、文件名的字符集
/*upload.setHeaderEncoding("GBK");*/
/*当文件名为汉字时 upload.setHeaderEncoding("GBK") 出现乱码 故改为utf-8*/
upload.setHeaderEncoding("utf-8");
// 设置允许用户上传文件大小,单位:字节
upload.setSizeMax(1024 * 1024 * 100);
// 解析请求,开始读取数据
// Iterator<FileItem> iter = (Iterator<FileItem>) upload.getItemIterator(request);
// 得到所有的表单域,它们目前都被当作FileItem
List<FileItem> fileItems = upload.parseRequest(request);
// 依次处理请求
Iterator<FileItem> iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField() && StringUtils.isNotEmpty(item.getName().trim())) {
// 如果item是文件上传表单域
// 获得文件名及路径
String fileName = item.getName();
if (fileName != null) {
// 如果文件存在则上传
InputStream in = item.getInputStream();
byte[] imageByte = new byte[(int) in.available()];
in.read(imageByte);
in.close();
String realPath = request.getRealPath("androidFile")+"/"+fileName;
File file2 = new File(realPath);
if (!file2.exists()){
File vDirPath = file2.getParentFile(); // new
vDirPath.mkdirs();
FileOutputStream fileOut = new FileOutputStream(file2);
fileOut.write(imageByte);
fileOut.close();
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
下载 手机端
public static void download(final SuperWebView superWebView,String severPath,String androidPath,String fileName){
HttpUtils http = new HttpUtils();
RequestParams requestParams = new RequestParams();
String appURL =""; //服务端负责下载类 需要传递serverPath和fileName
File file2 = new File(androidPath);
if (!file2.exists()){
http.download(appURL,
androidPath,
requestParams,
true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
false, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
public void onStart() {
progressDialog = new ProgressDialog(superWebView.getContext());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("["+downLoadName+"]下载中...");
progressDialog.show();
}
public void onLoading(long total, long current, boolean isUploading) {
progressDialog.setMax((int)total);
progressDialog.setProgress((int)current);
}
public void onSuccess(ResponseInfo<File> responseInfo) {
progressDialog.hide();
}
public void onFailure(HttpException error, String msg) {
ToolUtil.showShortTip((Activity) superWebView.getContext(),msg);
}
});
}
}
下载 电脑端
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String curFile = WebProcess.getParameter(request, "fileName");
String filePath = WebProcess.getParameter(request, "serverPath");
try{
filePath=new String(filePath.getBytes("iso8859-1"),"utf-8");
curV=new String(curV.getBytes("iso8859-1"),"utf-8");
}catch(Exception e){
e.printStackTrace();
}
response.setContentType("application/octet-stream");
response.setHeader("charset", "UTF-8");
response.setHeader("Content-Disposition", "filename="+curFile);
//得到文件大小
//读数据
byte data[] = null;
FileInputStream hFile = null;
try {
hFile = new FileInputStream(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
OutputStream out = response.getOutputStream();
int i=hFile.available();
response.addHeader("Content-Length", i+"");
byte[] buf = new byte[i];
hFile.read(buf);
out.write(buf);
hFile.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
}
return null;
}