PhoneGap 上传图片HTML 和服务器端端实现(JAVA)
4人收藏此文章, 我要收藏发表于8个月前(2013-01-24 10:12) , 已有593次阅读 ,共0个评论
HTML 代码 利用PhoneGap 自己实现的API FileTransfer 的 upload 代码 001
002
003
004
005
006
007
008
009
011
012
286
287
288
289
290
291
292
293
294
295
304
305
306
307
308
309 310
311
312
313 314
315
316
317
318
319
320
321
322
323
324 id="smallImage" src="" />
325 style="display: none; width: 240px; height: 240px;" id="largeImage"
326 src="" />
327
328
329
330
331
332
339
340
341
342
343
344
345
346
347
348
服务器端接收图片
01 import java.io.File;
02 import java.io.FileOutputStream;
03 import java.io.IOException;
04 import java.io.InputStream;
05 import java.io.OutputStream;
06 import java.util.List;
07
08 import javax.servlet.ServletException;
09 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.apache.tomcat.util.http.fileupload.DiskFileUpload; 14 import org.apache.tomcat.util.http.fileupload.FileItem;
15 import org.apache.tomcat.util.http.fileupload.FileUploadException; 16
17
18 public class PhoneGapServlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21
22 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 response.setContentType("text/html,charset=utf-8"); 24 response.getWriter().println("请以POST 方式上传文件"); 25 System.out.println("get.........");
26 }
27
28
29 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 30 response.setContentType("text/html;charset=UTF-8"); 31 // PrintWriter out = response.getWriter();
32 File file1=null;
33 DiskFileUpload disFileUpload = new DiskFileUpload(); 34 try{
35 @SuppressWarnings("unchecked")
36 List
37 for(FileItem fileItem:list){
38 if(fileItem.isFormField()){
39
40 }else {
41 if("fileAddPic".equals(fileItem.getFieldName())){
42 File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
43 System.out.println("开始遍历....."); 44
45 System.out.println(fileItem.getContentType()+"----"+remoteFile.getName()+fileItem.getName());
46
47 file1 = new File(this.getServletContext().getRealPath("attachment"),remot eFile.getName());
48 file1.getParentFile().mkdirs();
49 file1.createNewFile();
50
51 InputStream ins = fileItem.getInputStream(); 52
53 OutputStream ous = new FileOutputStream(file1);
54 try{
55 byte[] buffer = new byte[1024]; 56 int len=0;
57 while((len=ins.read(buffer))>-1){ 58 ous.write(buffer, 0, len); 59 }
60
61 }finally{
62 ous.close();
63 ins.close();
64 }
65 }
66 }
67 }
68 }catch(FileUploadException e){
69
70 }
71 }
72
73 }
这样就可以获取图片了,不过HTML 端获取的图片没有后缀名 可以在HTML 加入下面一段代码进行解析图片的实际路径
view source
print?
01 window.resolveLocalFileSystemURI(imageURI, onResolveSuccess, onError);
02
03 function onResolveSuccess(fileEntry){
04
05 alert(fileEntry.fullPath);
06 }
07 function onError(error) {
08
09 toLog("error code: "+ error.code);
10
11 };
HTML 代码是从网上搜到的,java 代码是最近做文件上传,利用的apache 的
commons-fileupload 实现的