小编Tin*_*iny的帖子

将图片添加到Excel单元格时,Apache POI-HSSF会扭曲图像大小

我正在使用Apache POI-HSSF将图片添加到单元格中.图像是120x100,但无论我做什么以及如何调整它,Excel电子表格总是显示它跨越多行并将其扭曲到比宽度更大的高度.

如何保持原始尺寸?

我的代码:

InputStream is = new FileInputStream(getImageURL());
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();

//add a picture shape
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
// Create the drawing patriarch.  This is the top level container for all shapes.
Drawing drawing = sheet1.createDrawingPatriarch();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it

anchor.setAnchorType(0);
anchor.setCol1(1);
anchor.setRow1(1);

Picture pict = drawing.createPicture(anchor, pictureIdx);

//auto-size picture relative to its top-left corner
pict.resize(); …
Run Code Online (Sandbox Code Playgroud)

java excel image hssf apache-poi

8
推荐指数
2
解决办法
8442
查看次数

Java中的条件运算符会抛出意外的NullPointerException

可能重复:
通过Java三元运算符的自动装箱行为的NullPointerException

以下代码使用简单的条件运算符.

public class Main
{
    public static void main(String[] args)
    {
        Integer exp1 = true ? null : 5;
        Integer exp2 = true ? null : true ? null : 50;

        System.out.println("exp1 = " +exp1+" exp2 = "+exp2);

        Integer exp3 = false ?  5 : true ? null: 50; //Causes the NullPointerException to be thrown.

        System.out.println("exp3 = "+exp3);
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码编译得很好.所有表达式最终试图向nullInteger输入变量exp1,exp2exp3分别.

前两种情况不会抛出任何异常并产生exp1 = null exp2 …

java nullpointerexception conditional-operator

7
推荐指数
1
解决办法
330
查看次数

Java中的replace()和replaceAll()

以下代码使用Java replace()中的String类的方法.

String a = "abc/xyz";
System.out.println(a.replace("/", "\\"));
Run Code Online (Sandbox Code Playgroud)

/在给定的String a中被替换为\.

如果我们使用replaceAll()如下方法,同样的事情是错误的.

System.out.println(a.replaceAll("/", "\\"));
Run Code Online (Sandbox Code Playgroud)

它会导致java.lang.StringIndexOutOfBoundsException抛出异常.它需要两个额外的反斜杠\,如下所示,因为replaceAll()使用正则表达式,而不是replace()方法的情况.

System.out.println(a.replaceAll("/", "\\\\"));
Run Code Online (Sandbox Code Playgroud)

唯一的问题是为什么这个方法只使用两个斜线就像这样a.replaceAll("/", "\\")抛出java.lang.StringIndexOutOfBoundsException


split()另一方面,该方法最初发布了一个waring Invalid regular expression: Unexpected internal error(我正在使用NetBeans 6.9.1).

String b="abc\\xyz";
System.out.println(b.split("\\")[0]+b.split("\\")[1]); //Issues a warning as specified.
Run Code Online (Sandbox Code Playgroud)

尝试运行此操作会导致java.util.regex.PatternSyntaxException抛出异常.

由于它使用正则表达式replaceAll(),它需要四个反斜杠.

System.out.println(b.split("\\\\")[0]+b.split("\\\\")[1]);
Run Code Online (Sandbox Code Playgroud)

为什么a.replaceAll("/", "\\\\");在前面的情况下不会发出这样的警告或这样的运行时异常,即使它有一个无效的模式?

java regex string replace replaceall

7
推荐指数
1
解决办法
6484
查看次数

在Java中使用带有String和Object的equals()方法

Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));
Run Code Online (Sandbox Code Playgroud)

它回来了false.true如果删除了注释,它可以返回.


为什么不适合String同班同学?

String s1=new String();
String s2=new String();
System.out.println(s1.equals(s2));
Run Code Online (Sandbox Code Playgroud)

它回来了true.为什么?(因为String使用实习生或其他涉及的东西?)

java string equals

7
推荐指数
3
解决办法
4万
查看次数

如何每隔一小时以固定的时间间隔删除会话数据?

我正在生成随机令牌,原因在于这个问题中提到的放在a中的内容java.util.ListList保存在会话范围内.

经过一些谷歌搜索后,我决定List在会话中每隔一小时删除所有元素(令牌).

我可以想到使用Quartz API,但这样做,似乎不可能操纵用户的会话.我在Spring中尝试使用Quartz API(1.8.6,2.x与我正在使用的Spring 3.2不兼容)可以在下面看到.

package quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public final class RemoveTokens extends QuartzJobBean
{    
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException
    {
        System.out.println("QuartzJobBean executed.");
    }
}
Run Code Online (Sandbox Code Playgroud)

它在application-context.xml文件中配置如下.

<bean name="removeTokens" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="quartz.RemoveTokens" />
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="5" />
        </map>
    </property>
</bean>

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
      <property name="jobDetail" ref="removeTokens"/>
      <property name="startDelay" value="10000"/>
      <property name="repeatInterval" value="3600000"/>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
      <list> …
Run Code Online (Sandbox Code Playgroud)

java session spring jsp quartz-scheduler

7
推荐指数
1
解决办法
6833
查看次数

java.lang.IllegalStateException:在JSF中实现自定义标记处理程序时出现java.lang.InstantiationException

给出以下标记处理程序类.

public final class ViewParamValidationFailed extends TagHandler implements ComponentSystemEventListener
{
    private final String redirect;

    public ViewParamValidationFailed(TagConfig config) {
        super(config);
        redirect = getRequiredAttribute("redirect").getValue();
    }

    @Override
    public void apply(FaceletContext context, UIComponent parent) throws IOException {
        if (parent instanceof UIViewRoot && !context.getFacesContext().isPostback()) {
            ((UIViewRoot) parent).subscribeToEvent(PostValidateEvent.class, this);
        }
    }

    @Override
    public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.isValidationFailed()) {
            try {
                ExternalContext externalContext = context.getExternalContext();
                externalContext.redirect(externalContext.getRequestContextPath() + redirect);
            }
            catch (IOException e) {
                throw new AbortProcessingException(e);
            }
        }
    } …
Run Code Online (Sandbox Code Playgroud)

jsf jsf-2.2

7
推荐指数
1
解决办法
3877
查看次数

JSF中具有单个命令组件的多个动作侦听器

是否可以使用单个命令组件调用多个侦听器方法?例如,

视图范围的bean:

@ManagedBean
@ViewScoped
public final class ViewScopedBean implements Serializable
{
    @ManagedProperty(value = "#{sessionScopedBean}")
    private SessionScopedBean sessionScopedBean; //Getter/Setter.
    private static final long serialVersionUID = 1L;

    public ViewScopedBean() {}

    public void action()
    {
        //Do something.
        sessionScopedBean.action();
    }
}
Run Code Online (Sandbox Code Playgroud)

会话范围的bean:

@ManagedBean
@SessionScoped
public final class SessionScopedBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    public SessionScopedBean () {}

    public void action() {
        //Do something.
    }
}
Run Code Online (Sandbox Code Playgroud)

一个命令按钮,如下所示,

<h:commandButton value="Action" actionListener="#{viewScopedBean.action}"/>
Run Code Online (Sandbox Code Playgroud)

调用该方法action(),ViewScopedBean然后通过注入该bean的实例来调用该action()方法SessionScopedBean.

是否有可能在XHTML上做同样的事情,以便可以消除只是为了调用方法而注入bean的需要?

jsf jsf-2

7
推荐指数
1
解决办法
1万
查看次数

在PrimeFaces(4.0)确认对话框中,如何确保默认情况下"否"是焦点?

从下图中可以看出,"是"按钮成为主要焦点.您是否可以确保在没有交换按钮的情况下关注"否",可能还有一个属性?

在此输入图像描述

jsf primefaces jsf-2

7
推荐指数
1
解决办法
1192
查看次数

在 MySQL 中设置密码错误

我正在尝试使用以下命令重置我的 root 密码:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('test');
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“PASSWORD('test')”附近使用的正确语法

请让我知道我在这里做错了什么。

mysql

7
推荐指数
2
解决办法
1万
查看次数

何时在JPA标准API中使用select子句?

  1. 不使用CriteriaQuery#select():

    public List<Address> getAddressOfManager(String designation, String name, String orderByColumn) {
    
        Boolean ascending = false;
        CriteriaBuilder cb = emanager.getCriteriaBuilder();
        CriteriaQuery<Address> cq = cb.createQuery(Address.class);
        Root<Address> root = cq.from(Address.class);
        //cq.select(root);  <-------------
        Join<Address, Employee> employeeAddress = root.join(Address_.employee);
        Join<Employee,Project> employeeProject = employeeAddress.join(Employee_.project);
        cq.where(cb.or(cb.equal(employeeProject.get(Project_.name), name),cb.equal(employeeAddress.get(Employee_.designation), designation)));
        Order order = ascending ? cb.asc(root.get(orderByColumn))
                : cb.desc(root.get(orderByColumn));
        cq.orderBy(order);
        List<Address> result = emanager.createQuery(cq).getResultList();
        return result;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. CriteriaQuery#select():

    public List<Address> getAddressOfManager(String designation, String name, String orderByColumn) {
    
        Boolean ascending = false;
        CriteriaBuilder cb = emanager.getCriteriaBuilder();
        CriteriaQuery<Address> cq …
    Run Code Online (Sandbox Code Playgroud)

hibernate jpa criteria criteria-api

7
推荐指数
1
解决办法
7233
查看次数