我的方法如下,
public byte[] readFlie(String filepath) {
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(filepath);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) { out.write(b, 0, n); }// end while } catch (Exception e) {
System.out.println(e); } finally { if (is != null) { try {
out.close();
is.close(); } catch (Exception e) { }// end try }// end if }// end try
return out.toByteArray(); }
我需要通过httppost.webservice上传文件,
需要将文件转换为byte数组,但是文件十兆以内不会有问题,超过了,基本上会内存溢出,请问有什么办法
public byte[] readFlie(String filepath) {
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(filepath);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) { out.write(b, 0, n); }// end while } catch (Exception e) {
System.out.println(e); } finally { if (is != null) { try {
out.close();
is.close(); } catch (Exception e) { }// end try }// end if }// end try
return out.toByteArray(); }
我需要通过httppost.webservice上传文件,
需要将文件转换为byte数组,但是文件十兆以内不会有问题,超过了,基本上会内存溢出,请问有什么办法
类似于:
public byte[] readFlie(String filepath) {
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(filepath);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
int temp = 0;
while ((n = is.read(b)) != -1) {
temp ++;
if(temp >= 100){
out.write(b, 0, n);
temp = 0;
}
}// end while} catch (Exception e) {
System.out.println(e);} finally {if (is != null) {try {
out.close();
is.close();} catch (Exception e) {}// end try}// end if}// end try
return out.toByteArray();}
这里是个小例子,告诉你怎么用MTOM,
http://www.mkyong.com/webservices/jax-ws/jax-ws-attachment-with-mtom/
http://weblogs.java.net/blog/adhirmehta/archive/2010/06/11/transferring-large-binary-data-web-services
http://topic.csdn.net/u/20110612/14/9c5eec66-5054-4309-9525-1281b7f4c98f.html