我现在调别人的接口,需要传一个zip包的路径。这个zip包有一个xml文件。我要做的是把xml文件压缩成zip包
我生成xml文件后,右键用WinRAR打的zip包。在调他们的接口,可以成功解析的我的xml文件。然而我用java程序去打包,这个时候生产的zip包。去调他们的接口,居然不认识我的zip包,解析不了我的xml文件。
我把zip解压出来看了,有我的xml,而且xml的内容没有乱码,没有错,很完整。为什么会有这样的情况。我快疯了。难道是打包的代码不对?我的代码生产的zip包,解压出来就是那个xml文件。我的错是哪里啊? 谁知道一下啊?拜托了!!能够把发个把xml打成zip包的代码给我更感谢!!

解决方案 »

  1.   

    如果你生成的zip文件可以用WinRar打开并解压成功,那么错误就是他们而不是你的是否你们约定的ZipEntry名字不一样(区分大小写)错误的异常是什么?
      

  2.   

    public class Compress { public static void main(String[] args) throws IOException {
    File file = new File("D:/build.xml");
    ZipOutputStream zipOutputStream = new ZipOutputStream(
    new FileOutputStream(new File("D:/zip/xxx.zip")));
    BufferedInputStream bufferdInputStream = new BufferedInputStream(
    new FileInputStream(file));
    byte[] buffer = new byte[1024];
    zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
    int length = 0;
    while (true) {
    length = bufferdInputStream.read(buffer);
    if (length < 0) {
    break;
    }
    zipOutputStream.write(buffer, 0, length);
    }
    zipOutputStream.flush();
    bufferdInputStream.close();
    zipOutputStream.closeEntry();
    zipOutputStream.close();
    }}
      

  3.   

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd"
    default-autowire="byType" default-lazy-init="true"> <!--
    The <http> element is responsible for creating a FilterChainProxy and the
    filter beans which it uses. Common issues like incorrect filter ordering are
    no longer an issue as the filter positions are predefined.
    -->
    <http auto-config="true" realm="isapweb Realm" access-denied-page="/accessDenied.jsp">
    <!--  <intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY" /> -->
            
    <!-- without privilege controller -->
            <intercept-url pattern="/img/**" filters="none" />
            <intercept-url pattern="/css/**" filters="none" />        
    <intercept-url pattern="/style/**" filters="none" />
    <intercept-url pattern="/images/**" filters="none" />
    <intercept-url pattern="/js/**" filters="none" />
    <intercept-url pattern="/dwr/**" filters="none" />
    <intercept-url pattern="/services/**" filters="none" />
        <intercept-url pattern="/security/login.do*" filters="none" />
    <!-- Expose Pms remoting service-->
    <intercept-url pattern="/remoting/PmsService" filters="none" /> 
    <intercept-url pattern="/logout.do" filters="none" />

    <intercept-url pattern="/init/setConfig.do" filters="none" />
    <intercept-url pattern="/init/loadInitConfig.do" filters="none" />     <intercept-url pattern="/**" access="ROLE_USER, ROLE_ADMIN" /> 
            <form-login login-page="/security/login.do" default-target-url="/portal/index.do" always-use-default-target="false" authentication-failure-url="/security/login.do" />
    <logout logout-success-url="/security/login.do" invalidate-session="true" />
    <concurrent-session-control expired-url="/security/login.do" />
    </http>
    <!-- 为所有用户配置  error 1:鉴权失败     2:会话过期       3:访问被拦截 -->
    <beans:bean id="loggerListener" class="org.springframework.security.event.authentication.LoggerListener" />

    <!-- dynamac privilege controller  -->
    <authentication-provider user-service-ref="userDetailsService" >
    <password-encoder hash="sha"/>
    </authentication-provider>

    <beans:bean id="userDetailsService" class="com.*.service.impl.UserDetailsServiceImpl">
    </beans:bean>

    </beans:beans>
      

  4.   

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.GrantedAuthority;
    import org.springframework.security.GrantedAuthorityImpl;
    import org.springframework.security.userdetails.UserDetails;
    import org.springframework.security.userdetails.UserDetailsService;
    import org.springframework.security.userdetails.UsernameNotFoundException;
    /**
     * Spring Security用户权限接口的实现类
     * 
     */
    public class UserDetailsServiceImpl implements UserDetailsService {    @Autowired
        private UserServerService userServerService;    /**
         * spring security 实现方法
         * 
         * @throws UsernameNotFoundException 当用户不存在或用户被禁用
         * 
         */
        @Override
        public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
            // 根据用户名查询用户及其权限
            UserDto user = (UserDto) userServerService.getUser(userName);
            if (null == user) {
                throw new UsernameNotFoundException(userName + " not exist!");
            }
            if (!user.isEnabled()) {
                throw new UsernameNotFoundException(userName + " is disabled!");
            }
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;
            GrantedAuthority[] grantedAuths = obtainGrantedAuthorities(user);
            org.springframework.security.userdetails.User userdetail = new org.springframework.security.userdetails.User(
                user.getUserName(), user.getPassword(), user.isEnabled(), accountNonExpired, credentialsNonExpired,
                accountNonLocked, grantedAuths);
            return userdetail;
        }    /**
         * 获得用户所有角色的权限.
         * 
         * @param User 用户
         * @return GrantedAuthority[] 用户权限列表
         */
        private GrantedAuthority[] obtainGrantedAuthorities(UserDto user) {
            String roles = user.getRoles();
            String[] rolesArray = roles.split(",");//数据库中存储格式:ROLE_ADMIN,ROLE_USER
            //rolesArray[0]="ROLE_ADMIN";
            GrantedAuthority[] authArray = new GrantedAuthority[rolesArray.length];
            for (int i = 0; i < rolesArray.length; i++) {
                authArray[i] = new GrantedAuthorityImpl(rolesArray[i]);
            }
            return authArray;
        }
    }
      

  5.   

    import org.apache.commons.lang.StringUtils;import flexjson.JSONException;
    import flexjson.JSONTokener;// flexjson.jar
    public class ConversionUtils {    /**
         * 将汉字转换为Unicode
         * 
         * @param inStr
         * @return
         */
        public static String str2Unicode(String inStr) {
            String result = new String("");
            if (inStr == null) {
                inStr = "";
            }        for (int i = 0; i < inStr.length(); i++) {
                char temChr = inStr.charAt(i);
                if (temChr > 128) {
                    result = result + "\\u" + Integer.toHexString(temChr);
                } else {
                    result = result + temChr;
                }
            }        return result;
        }    /**
         * 将Unicode转换为汉字
         * 
         * @param unicodeStr
         * @return
         */
        public static String unicode2Str(String unicodeStr) {
            if (null == unicodeStr) {
                return "";
            }        String v = "'" + StringUtils.trim(unicodeStr) + "'";
            try {
                unicodeStr = new JSONTokener(v).nextValue().toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return unicodeStr;
        }}