WebService上传文件
|
? ? ? 通过WebService把一个字符串形式的参数传递到服务器端的一个函数并取得返回的结果,我们都比较熟悉,而通过WebService上传文件的原理和上传一个字符串在根本上是一样的。
? ? ? 唯一不同的是,我们需要多做一点额外的工作,即先读取文件到一个字节数组中,再通过Base64将其转化为字符串。详情请看下面的代码: ? ? ? 客户端读取文件然后用Base64将其转化为字符串的函数 private static String getFileByteString(File file) throws Exception{
InputStream in = new FileInputStream(file);
// 取得文件大小
long length = file.length();
// 根据大小创建字节数组
byte[] bytes = new byte[(int) length];
// 读取文件内容到字节数组
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = in.read(bytes,offset,bytes.length - offset)) >= 0) {
offset += numRead;
}
// 读取完毕的校验
if (offset < bytes.length) {
throw new IOException("不能完全讀取文件:"+ file.getName());
}
in.close();
String encodedFileString = Base64.encode(bytes);
return encodedFileString;
}
? ? ?服务器端将接收到的字符串还原成文件 ? ? ?有了上面函数的帮助,我们把文件传递到WebSercvice端某函数是必能做到的事,剩下的问题是,如何将接收到的字符串还原成文件呢? ? ? ?下面是示例程序: // uploadedFileString是传过来的包含你上传的文件内容的字符串
byte[] bytes = Base64.decode(uploadedFileString);
// 存储路径
String path=CommonUtil.getUploadPath();
// 存储的文件名
String localFileName=getLocalFileName(parser.getUserid(),parser.getFileName());
// 写入新文件
FileOutputStream out = new FileOutputStream(path+localFileName);
out.write(bytes);
out.flush();
out.close();
? ? ? ?客户端访问已上传的文件 ? ? ? ? ? ?http://ip:8080/webApp/upload/1.jpg. 上面IP地址是WebSercice应用所在机器的公网地址,webApp是该应用名。 public class InitialSystemServlet extends HttpServlet {
private static final long serialVersionUID = -7444606086930580188L;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,java.io.IOException {
}
public void doGet(HttpServletRequest request,java.io.IOException {
doPost(request,response);
}
public void init(ServletConfig config) throws ServletException {
// 设置上传路径
CommonUtil.setUploadPath(config.getServletContext().getRealPath("/"));
}
}
? ? ? ?其它问题 ? ? ? ?1.如何防止文件被覆盖:在生成文件时采用时间+用户ID+随机数的文件名,这样重名几率就大大降低,还不放心可以在写文件之间检验文件是否已存在了。 ? ? ? ?2.如何要把文件不放在服务器而是放到数据库怎么办:你可以把文件内容甚至字符串直接存储到数据库,需要下载时再取出来。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
